Hey all
So I’m writing a little tank controller, nothing fancy or realistic, and I seem to be having issues rotating the rigidbody. At first transform.Rotate seemed to work but I don’t think it rotates the rigidbody. So now I’m trying rigidbody.MoveRotation and well… it doesn’t seem to work. My MoveTank function works fine but I can’t seem to get the darn thing to rotate. I must be doing something wrong here. Any help would be appreciated.
using UnityEngine;
using System.Collections;
public class TankController : MonoBehaviour {
public float speed = 2f;
public float rotateSpeed = 2f;
Vector3 movementZ;
Vector3 rotationY;
void FixedUpdate ()
{
float y = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
MoveTank (z);
RotateTank (y);
}
//Move rigidbody
void MoveTank(float z)
{
//Move forward
if (z > 0)
{
movementZ.Set (0f, 0f, z);
movementZ = movementZ.normalized * speed * Time.deltaTime;
rigidbody.MovePosition (transform.position + movementZ);
}
//Move backward half speed
else if (z < 0)
{
movementZ.Set (0f, 0f, z);
movementZ = movementZ.normalized * (speed / 2) * Time.deltaTime;
rigidbody.MovePosition (transform.position + movementZ);
}
}
//Rotate rigidbody
void RotateTank(float y)
{
rotationY.Set (0f, y, 0f);
rotationY = rotationY.normalized * rotateSpeed;
Quaternion deltaRotation = Quaternion.Euler(rotationY * Time.deltaTime);
rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
}
}