"transform.Translate" doesn't works
I have to set the control setting for a sprite in Unity 2d. It should be simple: "w" and "s" to move back and forth, "a" and "d" to rotate the sprite. The rotation works, but it doesn't move. Here it is the script:
public float movespeed;
public float turnspeed;
void Update()
{
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * movespeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(-Vector3.forward * movespeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(new Vector3(0, 0, turnspeed) * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(new Vector3(0, 0, -turnspeed) * Time.deltaTime);
}
}
Answer by Hellium · Jun 13, 2018 at 05:34 PM
Because you have a 2D game you have to use Vector3.right
instead of Vector3.forward
. The latter vector moves your gameObject on the Z axis. However, in 2D, you only work with the (XY) plane...
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.right* movespeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(-Vector3.right* movespeed * Time.deltaTime);
}
Answer by tormentoarmagedoom · Jun 13, 2018 at 04:24 PM
Good day.
Whats the value of movespeed and turnspeed? Its a 2D game right? what are the planes you are using? maybe forward is set to "up" so it will not move, i recomment you to write the Vector the same way you did in the rotation (0, 0, movespeed) or (0, movespeed, 0) or (movespeed, 0, 0)
Bye!
Your answer
Follow this Question
Related Questions
Car Controls without physics. 1 Answer
transform.translate limits 1 Answer
When I translate my character it go to a strange z position. 0 Answers
rotate object2d the direction it is moving? 0 Answers
Can't move instantiated object. 1 Answer