Build Crossy Road Replica Part-5
if you play now . when your car hits the player it stops, because it velocity was changed to zero.
But If you analyse the game , actually there is no need for collision , instead we need only an output saying the vehicle hit the chicken .
So to do that what you have to do is check the Is Trigger in the collider of the vehicle.
What this does is that , Now the vehicle will not collide instead it will call a function( a particular block of code ) when it collides with other object. So that now we can run any code as we required when the two objects meet.but instead of doing any complex task we will just print "Hit" to the console tab.
so note that this function can be written inside any script attached to it.And we are writing it inside the vehicle script.
the name of that function is OnTriggerEnter() , which will be called at the frame when the player enters the vehicle collider.
And the final code will be like:
inside OnTriggerEnter we have written Debug.log("Hit"); so that when ever the player hit the vehicle "Hit" will be output on the console tab.
Save and try it on the game.
Now next , we need to reset the position of our vehicle after some time so that it loops continuously .for that what we do is that
first during the start we save the initial position to a variable.
then we declare two other variables ( tmint,temptm ) for the time interval and other for saving the current time
during each update
we compare if the current time is greater the previously set time . if so we will get the next set time as current time + time interval..And run a following task ( ie reset our vehicle position and velocity ) so that this task will be repeated after every time interval.
Now the vehicle code be like:
Now if you save and go to unity editor you will see Tm_interval and Nexttm under the vehicle script component in the inspector tab. set value only for Tm-interval , since NextTm is calculated automatically. for me my car need to reset at 5 sec so mine was 5 forTm-interval.
Now if you play you will be able to see you car resets after the time interval .
No comments: