Character goes back to a set rotation every time I stop moving.
Hello. The title basically covers my problem. When I stop pressing a key to move, my character just goes right back to looking straight ahead. This is a huge problem because I am trying to set up a third person camera, but if it keeps going back to the same rotation once you stop moving, its really annoying. If anyone would help me that would be awesome. Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5;
private Rigidbody rb;
public LayerMask groundLayers;
public float jumpForce = 4;
public CapsuleCollider col;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
}
// Update is called once per frame
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical).normalized * speed * Time.deltaTime;
float targetAngle = Mathf.Atan2(movement.x, movement.z) * Mathf.Rad2Deg;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
rb.MovePosition(transform.position + movement);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
if (isGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private bool isGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x, col.bounds.min.y, col.bounds.center.z), col.radius * .9f, groundLayers);
}
}
Comment