How can I translate an object smoothly on the X Axis only? [C#]
I seem to be having an issue getting a user controlled object to translate along the X axis only via arrow keys. I've tried figuring it out and eventually succeeded but it moves way to fast and I don't know how to slow it down. Sorry for the noobie question.
public float speed = 50f;
public float upSpeed = 0.0015f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey ("left"))
{
transform.position = transform.position + Vector3.left ;
}
if(Input.GetKey ("right"))
{
transform.position = transform.position + Vector3.right;
}
}
void FixedUpdate ()
{
GetComponent<Rigidbody2D>().AddForce (Vector3.up * upSpeed);
}
Any help would be great and much appreciated.
Answer by Statement · Oct 09, 2015 at 11:42 PM
Try multiplying the direction with Time.deltaTime, which expresses the elapsed time since last frame in seconds. It can be confusing to get it if you are starting out but it can be helpful to think of its properties like this: If you multiply 1 (for example) by Time.deltaTime, you can think of it being a change of 1 per second.
transform.position = transform.position + Vector3.left;
...can be written shorter as...
transform.position += Vector3.left;
...can be written cleaner as...
transform.Translate(Vector3.left);
Translate basically means "move", "offset" or "pan" in this context. And to make your code respect timing in Unity, you should multiply the translated value by Time.deltaTime:
transform.Translate(Vector3.left * Time.deltaTime);
Vector3.left and Vector3.right are (-1, 0, 0) and (1, 0, 0) respectively, so the multiplication becomes (-1, 0, 0) * Time.deltaTime
in the previous code snippet. That is to say that you will now translate left by 1 meter per second. If you had not multiplied by Time.deltaTime, we'd been translating by one meter per frame. That can be both fast or slow, depending on your machine. For example a slow computer might be chugging along at 20 frames per second and a fast computer might be racing at 120 frames per second. The net effect if you had not used Time.deltaTime, on one computer you'd be moving 20 meters per second and on another computer you'd be moving 120 meters per second.
To include your configurable speed into this, we can multiply that into the translate too:
transform.Translate(Vector3.left * speed * Time.deltaTime);
So the final script could look like this:
public class Example : MonoBehaviour
{
public float speed = 50f;
public float upSpeed = 0.0015f;
void Update()
{
if (Input.GetKey("left"))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey("right"))
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().AddForce(Vector3.up * upSpeed);
}
}
But we can reduce the code a little more if you want by using an input axis:
public class Example : MonoBehaviour
{
public float speed = 50f;
public float upSpeed = 0.0015f;
void Update()
{
var direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
transform.Translate(direction * speed * Time.deltaTime);
}
void FixedUpdate()
{
GetComponent<Rigidbody2D>().AddForce(Vector3.up * upSpeed);
}
}
Thanks for the help. For some reason
var direction = new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
transform.Translate(direction * speed * Time.deltaTime) ;
was only allowing the object along the y Axis , so i simply shifted it over in the the y co ordinate like so
var directiony = new Vector3(Input.GetAxisRaw("Vertical"), 0, 0);
transform.Translate(directiony * speed * Time.deltaTime) ;
and it seems to be working just as I wanted it to. Thank you so much for your help and input!!
Your answer
Follow this Question
Related Questions
How to move model on x axis in increments of 1? Starting from zero at a range of -2 to 2. 2 Answers
How To Make My Player Only Move on the ground? 1 Answer
(Newbie's First Project) Object doesn't stop moving until long after I let go of the button 1 Answer
Regarding transform.position in the roll a ball tutorial 0 Answers