Question by
timeslider · Dec 02, 2016 at 03:41 AM ·
rotationrigidbodyquaternionscale
How can I get a rigid body object to face in the direction my joystick is pointing smoothly?
I finally got it to look in the direction of my joystick but I'm clueless when it comes to making it smooth. I tried using Quaternion.Lerp but I couldn't get it working. Here's a simplified version of the code so far:
public class heroMove2 : MonoBehaviour
{
private float speed;
private Rigidbody heroRigidBody;
void Start()
{
heroRigidBody = GetComponent<Rigidbody>();
speed = 5.0f;
}
void FixedUpdate()
{
if ((Input.GetAxis("Horizontal") != 0) || (Input.GetAxis("Vertical") != 0))
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
heroRigidBody.velocity = movement * speed;
Quaternion rotation = Quaternion.Euler(new Vector3(0.0f, -Mathf.Atan2(moveVertical, moveHorizontal) * Mathf.Rad2Deg, 0.0f));
heroRigidBody.MoveRotation(rotation);
}
}
}
Comment