When I was creating Nightmare Six, the thing that I just couldn't get right for some reason was how to make an object forwards or backwards without it tipping over. Some people are probbably laughing at this but it's usually the easiest things that I get stuck on . Now that I have found out the solution to my problem, I will try my hardest to explain this is the easiest way possible. This is especially useful if you want to make a box (or something similar) move across the room when entering a trigger.
If you are reading this post from the homepage and you are wanting to know how to move an object forward, just click on the post's title and it should navigate you to the full post.
1) First of all, give the game object you want to move a rigid body. This can be done by pressing Components -> Physics -> Ridgidbody
2) Now that the game object has a rigid body, its now time to create a script that will allow the box to move forward. Create a javascript called moveforward.js and then enter the following code inside of that file -
#pragma strict
function Start () {
}
function Update () {
rigidbody.velocity = transform.forward * 35;
}
Now, you see how the word "forward" is in bold? Because that word is basically telling the object which way to go. By going foward, the object is moving along the blue line (when set in local), because that blue line represents which way the game object is facing.
There are 3 different words you can put here which determine the position of the object.
transform.forward (blue line)
transform.up (green line)
transform.right (red line)
Now what happens if you want the game object to go backwards? or down? or left? well it's simple. You just have to get the oppisite of where you want it to go, and make the number a negative.
For example, if I wanted my object to move backwards, I would enter the word forward (because it's the opposite of backwards) and put the number into negative.
transform.forward * -35;
3) Now that you have your script sorted out, you must now put the script (moveforward.js) on the object that you want to move.
4) Now if you play the game, you will see that the object will be moving forward, but it will be tipping and turning all over (If its a cube). This is where I got stuck at. The problem is that the rididbody that you enabled earlier has gravity ticked, so it's basically going by the laws of gravity.
To fix this, simply go to the game object and on the right there should be a little tick box with gravity besides it. Simply untick it and it will be fixed!
Now I will be explaining how to make this object move forward when you enter a trigger.
5) To make things a tiny bit easier, rename the object that you want moving to something easy like "MovingObject1" or something that you can remember.
6) Now you need to click on the game object that you want moving and disable the script which makes the object move (for this tutorial I called it moveforward.js). You can disable it by pressing the little tick box next to the script name in the inspector.
7) Now make a cube object and name it Trigger1 then place it where ever you want on your game. This cube is going to be the trigger as when the player walks into where the cube is located as, a trigger will be set off.
8) Now that you have placed your cube, look in your inspector and make the cube a trigger, this can be done by clicking on "Box Collider" and clicking on the tick box "Is Trigger" also you must untick the "Mesh Renderer" box because if you don't the trigger's location will be revealed with a massive cube.
9) Now that we now have our trigger sorted out, its time to create a script. Make a JavaScript called Trigger1.js (It doesn't have to be called Trigger1.js, I'm just using this name as an example)
In that JavaScript, replace everything with the following -
function OnTriggerEnter(){
if(!hasPlayed){
gameObject.Find("MovingObject1").GetComponent(moveforward).enabled = true;
}
}
As you can see, i have put in bold important words which I will explain about below.
First of all, the word MovingObject1 is the game of the game object that has the moving script attached. In this post, I named it MovingObject1.
The bit where it says GetComponent(moveforward) is the part where you tell the game which script you want enabling, in this post I named it moveforward.js so I enter moveforward (don't include the .js)
7) Now simply save that javascript file and drag it onto the game object that is a trigger. I called my game object "Trigger1"
8) Now if you play it, you will notice that the object is not moving, but if you walk to the location of that trigger, it will start moving.