0
\$\begingroup\$

My character is facing the direction its moving to, but when it stops moving, it rotates back to facing the world z axis.It rotates back to "how it was".

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ThirdPersonController : MonoBehaviour
{
    public float walkSpeed = 6.0f;
    public float runSpeed = 11.0f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float speed = 6f;
    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;
    private Vector3 moveDirection;
    private CharacterController controller;
    private bool isGrounded = false;

    private void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    private void Update()
    {

        if (controller.isGrounded)
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                speed = runSpeed;
            }
            else
            {
                speed = walkSpeed;
            }

            float horizontal = Input.GetAxis("Horizontal");
            float vertical = Input.GetAxis("Vertical");
            moveDirection = new Vector3(horizontal, 0, vertical).normalized;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
            float targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), Time.deltaTime * turnSmoothTime);


        }
        moveDirection.y -= gravity * Time.deltaTime;

        controller.Move(moveDirection * speed * Time.deltaTime);

    }
}

please help and thanks.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$
// targetAngle gets declared *outside* your Update function,
// so it can remember the angle value between runs.
float targetAngle;

private void Update()
{
     // ...skipping over some unrelated code...

    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    moveDirection = new Vector3(horizontal, 0, vertical).normalized;

    // Choose a new `targetAngle` only if the movement input is non-zero.
    // Otherwise, keep using the angle chosen last frame.
    if (moveDirection != Vector3.zero) {
        targetAngle = Mathf.Atan2(moveDirection.x, moveDirection.z) * Mathf.Rad2Deg;
    }

    // No float angle = ... line at all (you never used its value anyway)

    transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0f, targetAngle, 0f), Time.deltaTime * turnSmoothTime);

    if (Input.GetButton("Jump"))
    {
        moveDirection.y = jumpSpeed;
    }
\$\endgroup\$
4
  • \$\begingroup\$ could you please clarify a bit more? I implemented what you gave me but its giving me the same result. I moved the " float targetAngle=" and " float angle=" up before the "f (moveDirection != Vector3.zero)" \$\endgroup\$
    – Solodobo
    Feb 4, 2023 at 22:36
  • \$\begingroup\$ Don't do that, obviously. If it was supposed to be written before, I'd have written it before, not after. Your angle = ... was declaring a variable you never use, so that can be deleted entirely. \$\endgroup\$
    – DMGregory
    Feb 4, 2023 at 23:23
  • \$\begingroup\$ I moved it up because I was getting an error on my targetAngle (cannot use local variable "targetAngle before it is declared) but I see your solution. thanks man it all works good! \$\endgroup\$
    – Solodobo
    Feb 4, 2023 at 23:49
  • \$\begingroup\$ That's why the comment told you to do exactly what I've shown here. Make sure you read those next time. 😉 \$\endgroup\$
    – DMGregory
    Feb 5, 2023 at 0:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .