So I’m making a 2D game in which the player object can (or should be able to should I say) only be rotated around its center (with arrow / A/D keys) and thrusted in the direction it’s facing. I’ve found some tutorials on how to translate mouse movement to camera rotation and then movement, but none of it seemed to work in my case. What’s the best way I should go about this?
Sounds like you should just watch a few basic tutorial videos and get used to unity.
This is something very simple.
public GameObject player;
public float speed = 0.1f;
void FixedUpdate () {
if (Input.GetKey("a"))
{
player.transform.Rotate(1f, 0f, 0f);
}
if (Input.GetKey("d"))
{
player.transform.Rotate(-1f, 0f, 0f);
}
player.transform.position += player.transform.forward * speed;
}
Thanks for replying. I know how to rotate the object, the problem is that I want the “forward” direction to rotate along with it, just like in the old Asteroids arcade. Could you help me with that?
“Forward” is part of the transform. If you want to get the new forward direction, use transform.forward.
transform.forward may not work because it represents the Z axis. It depends on your world orientation. If you need 2D physics, Z “forward” will be depth because 2D physics only works in the XY plane.
If using 3D physics from a top-down perspective, rotating about the Y axis and moving in the direction of transform.forward should work.
I don’t quite understand what you mean. Isn’t that exactly what the script is doing.
You can use transform.right or transform.up if you need the X or Y axis instead.
Okay thank you everyone, transform.up was what I was looking for. I always thought those just produced x/y/z = 1f for some reason. Transform.forward made everything weird and confused me because it moves the object along the Z axis and I’m working with 2D.
Glad you figured it out. You were likely confusing transform.forward with Vector3.forward. All the “Vector3” ones are just shorthand for axis unit vectors.
Yep that’s what happened, I’ve already looked the stuff up. Thanks