Hi,
I’m stuck with probably very easy problem. I want my character to move like in LEGO series, where with left stick you can move character in every direction. When you change direction, character rotates towards the direction of movement. I tried every solution on internet and nothing works.
Right now I’ve ended up with this code
public class Player : MonoBehaviour {
public float speed = 10f;
public float Rspeed = 80f;
public float gravity = 10f;
private Rigidbody controller;
private Vector3 movement = Vector3.zero;
void Start () {
controller = GetComponent<Rigidbody>();
}
void Update () {
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis);
movement = transform.TransformDirection(movement).normalized;
movement *= speed * Time.deltaTime;
controller.MovePosition(movement);
if (movement != Vector3.zero) {
Vector3 turn = (controller.transform.position - transform.position);
Quaternion turnRotation = Quaternion.LookRotation(turn);
Vector3 rotation = Quaternion.Lerp(transform.rotation, turnRotation, Time.deltaTime * Rspeed).eulerAngles;
transform.rotation = Quaternion.Euler(0f, rotation.y, 0f);
}
}
}
Can anyone please help me solve this?