Build Crossy Road Replica Part-5

 



Now we need to make collision between our character and vehicle . 

We use colliders for that. these are like walls.So it dosen't allow any other object with collider component to pass through it instead it will collide.


We need to add collider to both our vehicle and game character for collision between them.

So First select anyone of the object and inside   inspector click add component -> type Box Collider and select it.  You will be able to see a green box around that object , which is the collider. Now we added box collider but unity have other shapes and you can make a collider shape of any object. but when the complexity of the collider shape increase . the performance decreases.

the size of the collider is automatically determined by Unity when you add it. but if you need you can adjust it in the inspector.

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:


using UnityEngine;

public class vehicle : MonoBehaviour
{

    public Vector3 vel;
    void Start()
    {
        this.GetComponent<Rigidbody>().velocity= vel;
    }


    void OnTriggerEnter()
    {
        Debug.Log("Hit");
    }

    void Update()
    {
        
    }
}

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:



using UnityEngine;

public class vehicle : MonoBehaviour
{

    public Vector3 vel,ps;// variables for velocity and position
    public float tm_interval,Nexttm; // initialize variable for saving the time
    void Start()
    {   
       ps = transform.position; // save our initial position
        this.GetComponent<Rigidbody>().velocity= vel;

    }


    void OnTriggerEnter() // function to display "Hit"
    {
        Debug.Log("Hit");
    }

    void Update()
    {
        if(Time.time>Nexttm) // run the below code only if current time is > nexttime
        {
            Nexttm=Nexttm+tm_interval; // if so find the next nexttm
            transform.position=ps; // reset the position
            this.GetComponent<Rigidbody>().velocity= vel;// reset velocity
        }
        
    }
}


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:

Powered by Blogger.