Hi,
Im learning how to use unity and started on a little project to get familiar with coding in unity and using the engine.
I’m trying to create movement for my player and the script that i have now works like i want it to, however my cube moves around quite fast so i want to limit the speed. I couldnt think of a way to do this though so i wonder how you would do it.
It would also be appreciated to tell me if im doing this character movement the correct way or if theres a better way of going about it.
Heres my code:
public float rotationSpeed = 20f;
public float maxSpeed = 0.7f;
Vector3 m_movementVec;
Quaternion m_rotation = Quaternion.identity;
Rigidbody m_rigidbody;
// Start is called before the first frame update
void Start()
{
m_rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Get user input
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Create vector from user input and normalize
m_movementVec.Set(horizontal, 0f, vertical);
m_movementVec.Normalize();
//Calculate what faces forward
Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_movementVec, rotationSpeed * Time.deltaTime, 0f);
// Set the rotation var
m_rotation = Quaternion.LookRotation(desiredForward);
//Set the new positions
m_rigidbody.MovePosition(m_rigidbody.position + m_movementVec);
m_rigidbody.MoveRotation(m_rotation);
Thanks !