#Turn smoothly

1 messages · Page 1 of 1 (latest)

ocean drift
#

Hi, I created a third-person controller. I was wondering if anyone could help me make this character turn smoothly.

Here is the code:

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    public float jumpForce;

    [Space(15)]
    public float checkDistance;
    public Transform groundCheck;
    public LayerMask groundMask;

    [Space(15)]
    public Transform playerMesh;

    [Space(15)]
    public bool canJump;
    public bool canMove;


    private void FixedUpdate()
    {
        Cursor.lockState = CursorLockMode.Locked;

        Rigidbody rb = GetComponent<Rigidbody>();

        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 forward = Camera.main.transform.forward;
        Vector3 right = Camera.main.transform.right;

        forward.y = 0;
        right.y = 0;

        forward.Normalize();
        right.Normalize();

        Vector3 moveDirection = forward * verticalInput + right * horizontalInput;

        rb.velocity = new Vector3(moveDirection.x * speed, rb.velocity.y, moveDirection.z * speed);

        if(moveDirection != new Vector3(0, 0, 0))
        {
            playerMesh.rotation = Quaternion.LookRotation(moveDirection);
        }

    }

    private void Update()
    {
        canJump = Physics.CheckSphere(groundCheck.position, checkDistance, groundMask);

        if (canJump && Input.GetButtonDown("Jump"))
        {
            Rigidbody rb = GetComponent<Rigidbody>();
            rb.velocity = Vector3.up * jumpForce;
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(groundCheck.transform.position, checkDistance);
    }
}

I used quaternion.lookatrotation, but that is super snapy. Can someone teach me how to incoporate Quaternion.Lerp or Slerp?

green marsh
#

Try using Quaternion.RotateTowards instead of directly setting playerMesh.rotation to Quaternion.LookRotation.

#

You wouldn't want to use Lerp or Slerp here, because you don't have a fixed start or target rotation.

ocean drift
#

yea i figured

#

ill try this

#

thanks!

#

playerMesh.rotation = Quaternion.RotateTowards(moveDirection);

#

this?

#

there is a red underline under RotateTowards

#

do you know how to fix this?

gusty dove
#

Apart from that, the error always tells you what's wrong, so use that too to find out what's wrong with your code.

ocean drift
#

ill take a look

#

thanks!

ocean drift
#

how could I use this in my code?

amber ravine
# ocean drift how could I use this in my code?

The example code on the docs page is pretty precisely what you need. Just replace the target with the rotation you want to rotate towards (the value you earlier set to transform.rotation)

gusty dove
#

The arguments you've passed are wrong. Try to solve that before you continue. The documentation should be pretty clear, even though it is often criticised. In this regard, it should be sufficient to show you what's wrong.

ocean drift
#

i c

ocean drift
#

in my case, what would be my target.rotation?

#

i tried moveDirection but its not working

#

i then tired rb.rotation, but now the cube that is a child of the player is not turning

ocean drift
#

i probably just dumb

#

but I can't figure it out

amber ravine
ocean drift
#

my game is third person

#

it want to face forward determining on the the rotation of the thirperson camera