Build Crossy Road Replica Part-7

 


In this post we are trying to set up the script in such a way that the unity would consider collision only when our player hits the vehicle this is because ,till now our script is in such a way that it would consider collision if any other object collides with the vehicle , so even if there is collision between two vehicle unity would consider it as a collision and prints "hit".

To fix that, we modify the "vehicle.cs" script in such a way that when the collision occurs it will check if the name of the collided object is our player name then only it would consider as a collision.

to do that we add a "Collider " variable in our OnTriggerEnter()  Function ,so it becomes : OnTriggerEnter(Collider xyz). 

Now the colliding object is stored in the xyz variable.and next is to check if its name is same as our players.

{ Note that we have removed the "Hit "printing function}

For that we use if statement, which compares if the name of that collider is "Chick"

which will be like:

if(xyz.name=="Chick")
{

//runs these code only if the above condition is true

}

so our vehicle.cs script becomes:


using UnityEngine;


public class vehicle : MonoBehaviour
{

    public Vector3 vel,ps;
    public float tm_interval,Nexttm;
    void Start()
    {   
       ps = transform.position;
        this.GetComponent<Rigidbody>().velocity= vel;

    }


    void OnTriggerEnter(Collider xyz)
    {
        if(xyz.name=="Chick")
        {
            
        }
    }

    void Update()
    {
        if(Time.time>Nexttm)
        {
            Nexttm=Nexttm+tm_interval;
            transform.position=ps;
            this.GetComponent<Rigidbody>().velocity= vel;
        }
        
    }
}


Now if you consider the original game , it will be like a when it collides . it pause the game and asks for what to do next like "go to main menu, restart etc." but for simplicity what we do is just reload the game.

Reloading Game:

Even though i have said "Reloading Game" , it is not the complete game which reloads , instead it one particular scene that reloads. Actually there no reload function available in unity instead what we  does is , we load the same scene again.So our scene changes its state to the initial state .

The use of scene method helps developers to reduce the complexity of the level design , ie each level(/ scene) can be designed separately and  loaded using scripts. And this can be seen in many game like , GTA San Andreas, where you will see a break when entering into a building, what's happening behind is that outside environment is removed from the memory and the inside environment is loaded into the memory.

 Loading scene :

Before writing functions for loading scene , note that these functions are currently not available in our script , because if you check the script , you will find that only " using UnityEngine; "is written , what gives us only the essential unity tools / functions. To avail scene management functions/tools we must explicitly mention it like:

using UnityEngine.SceneManagement;

Note that this is an extra line of code added to the script.So don't remove "using UnityEngine;"


Now we have the scene management tools available in our script.

To load scene we use:

SceneManager.LoadScene("SampleScene");  inside the if statement .

where "SampleScene" is the name of our scene , which is the name of the scene file you saved.

you will also able to find the name from the top window bar:



and now our full script will be like:


using UnityEngine;
using UnityEngine.SceneManagement;//new

public class vehicle : MonoBehaviour
{

    public Vector3 vel,ps;
    public float tm_interval,Nexttm;
    void Start()
    {   
       ps = transform.position;
        this.GetComponent<Rigidbody>().velocity= vel;

    }


    void OnTriggerEnter(Collider xyz)
    {
        if(xyz.name=="Chick")
        {
            SceneManager.LoadScene("SampleScene");//new
        }
    }

    void Update()
    {
        if(Time.time>Nexttm)
        {
            Nexttm=Nexttm+tm_interval;
            transform.position=ps;
            this.GetComponent<Rigidbody>().velocity= vel;
        }
        
    }
}

Now if you save and go to unity , you will be able to see that your game will reload if your player collides with the car.

And it doesn't consider the collision between vehicle.

Also note that there is a chance that you may face an error about the Scene ,For our version it is not showing error , if you are facing that error ,what you should do is to add your scene to the build scenes,

Adding scene to build:

Go to File: ->   Build Settings - Add open Scenes





And that should fix it.

No comments:

Powered by Blogger.