Build Crossy Road Replica Part-4
In this session we are going to add vehicles,
for that first drag any vehicle 3d file from the project tab.
next we need to make the vehicle move for that a single line of code is enough. So create a script with name vehicle :
this is the code you have to add
this.GetComponent<Rigidbody>().velocity= new Vector3(0,0,1f);
what this does it that during the starting of the game , the script will try to get a rigidbody component from the Gameobject we add it to . this is because the velocity of a game object can only be accessed from a rigidbody.
If it finds the rigidbody component it will assign a new given value to the velocity of that object.
since we need to assign different values for velocity instead of directly giving value , we create a variable called vel. which we will give values from the unity editor
public Vector3 vel;
Save the script and go back to unity , And now if you have not added script to the vehicle , do it by dragging it to vehicle. now you will be able to see val in the inspector . give it a value since i plan to move this in z direction according to my game I give values x=0,y=0,z=1.
But if you click play now. you will get an error because, as mentioned above our script will search for a RigidBody in our gameObject( here vehicle) , but it doesn't have one. So add one , {click "Add component" on inspector tab while selected vehicle object and type RigidBody (Not RigidBody2D ) select the Rigid Body shown there.}
And it will be added to your object. By default when ever you add rigidbody, gravity is enable. So our car moves down, which we don't want . So disable it by unchecking the Use Gravity in rigid body.
Now if you play the game our vehicle moves and we can move our character.
No comments: