- Home /
transform.Translate tutorial question
I was "playing" around with this tutorial: http://unity3d.com/learn/tutorials/modules/beginner/scripting/translate-and-rotate
I ended up with this code:
using UnityEngine;
using System.Collections;
public class TranformUp : MonoBehaviour {
public int velocity = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.W))
transform.Translate(new Vector3(0 , 5 ,0) * velocity * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.DownArrow) || Input.GetKeyDown (KeyCode.S))
transform.Translate (new Vector3(0 ,-5, 0) * velocity * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.LeftArrow) || Input.GetKeyDown (KeyCode.A))
transform.Translate (new Vector3(-5, 0, 0) * velocity * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.RightArrow) || Input.GetKeyDown (KeyCode.D))
transform.Translate (new Vector3(5, 0, 0) * velocity * Time.deltaTime);
}
}
What I don't understand is, that whatever value the variable velocity has, the result is the same.Isn't it supposed to multiply with the vector3 values?
EDIT: Even setting it to 0 doesn't change something
Answer by HuskyPanda213 · Dec 30, 2013 at 09:54 PM
The value is aslways the same because you go to a new vector 0,5,0 I would use vector3.forward or so. Then multiply.
Answer by sparkzbarca · Dec 30, 2013 at 09:59 PM
velocity is basically the speed at which it moves
the vector(0,5,0) etc is, well actually its part of the overall true velocity
a velocity is a speed in meters per second FOR EACH AXIS
so a velocity of (0,5,0) is a speed of 5 meters per second in the up direction
then you multiply this by your speed which is 10 and you get
0 10; 5 10; 0 * 10
and you end up with (0,50,0) which is confusing, its actually confusing because you shouldnt use 5 there in all those new vectors you should use 1, or actually you should use the objects local left, right, up and down
this would be a properly coded example
if (Input.GetKeyDown (KeyCode.RightArrow) || Input.GetKeyDown (KeyCode.D))
transform.Translate (transform.right * velocity * Time.deltaTime);
this will now take transform.right, transform.right is a direction, its a point exactly 1 unit to the right of the object.
so if its 1 unit away then when we multiply by 10 for example we get a unit 10 meters away and thats what we do for speed
so if we have it at 1 unit away and leave it and it takes 10 seconds to complete we go 10% along the line each time and travel at 1/10th meter per second
if we have it 10 units away so (transform.right 10 time.deltatime) it still takes 10 seconds to comlpete the journey but 10% along this line is further away so it travels faster it goes 1 meter per second.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125159im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UnityEngine.Component:get_transform() Error 1 Answer
Different between transform.Translate() and Vector3.MoveTowards? 1 Answer
Move child objects in the opposite direction of parent object 0 Answers
How the player can translate 1 "unit" and not smouthly. 2 Answers
Move GameObject along a line according to the players look direction 3 Answers