so I want to make an object turn left and right with the arrow keys. How do I do it?
there’s lots of ways, i’ll leave a link to a useful unity tutorial video but for the short answer.
you have transform.rotate
transform.Rotate(Vector3.up * Time.deltaTime);
this will rotate an object around the y axis at a speed of 1 every second, check Unity - Scripting API: Transform.Rotate for more info.
you also have rigidbody.AddTorque
Rigidbody.AddTorque(vector3.up * torque);
this is used to add rotation to rigidbodies in unity. check out Unity - Scripting API: Rigidbody.AddTorque for more
If you want to controll rotation using arrow key input, you can use.
void Update()
{
float h = Input.GetAxisRaw("Horizontal") * time.deltaTime
float v = Input.GetAxisRaw("Vertical") * time.deltaTime
transform.Rotate(v,h,0)
}
https://unity3d.com/learn/tutorials/topics/scripting/translate-and-rotate
https://unity3d.com/learn/tutorials/topics/physics/adding-physics-torque
These explain it very well.
hope this helped.
Hey man i am new too the whole helping people on Unity. And movement was something i got stuck with too when i just started with programming. So i got you a script that lets you move forward and backwards. And you can rotate with the left and right or the a and d keys. But dont just copy and paste it try too understand what the code does.
private float horz;
private float vert;
private float rotSpeed = 90.0f;
private float movSpeed = 3.0f;
private Transform thisTransform = null;
void Start(){
thisTransform = GetComponent<Transform>();
}
void Update(){
horz = input.GetAxis("Horizontal");
vert = input.GetAxis("Vertical");
thisTransform.rotation *= Quaternion.Euler(0f, horz * rotSpeed * time.deltaTime, 0f);
thisTransform.position += transform.forward * vert * movSpeed * time.deltaTime);
}