- Home /
How to make an object move 1 axes unit.
Sounds simple I know, but i'm new to programming ( using java ) so take it easy on me.
So I made a sphere and manually moved it "up" one unit. I created a java script and I have:
#pragma strict
import UnityEngine;
function Start () {
}
function Update () {
if( Input.GetKeyDown ( "w" ) )
transform.Translate( Vector3.forward*Time.deltaTime, Space.World );
}
I applied the script to the object and when I run the game and press "w" ... nothing. I know this is easy and i know i'm just ignorant to unity as of now but, any thoughts?
Thanks
Answer by robertbu · Mar 09, 2014 at 03:32 AM
Input.GetKeyDown() returns true for a single frame. Assuming your game is running at 60 FPS, this code will move your object 1/60 of a unit during that single frame (since Time.deltaTime will be approximately 1/60), and then nothing afterwards. If you remove the 'Time.deltaTime', this code will give you an immediate move of one unit 'forward' (i.e. in the positive 'z' direction). To move up, replace 'Vector3.forward' with 'Vector3.up'.
Note if you change the GetKeyDown() to GetKey() (and leave the Time.deltaTime), your code will move smoothly as long as the key is held down.