#My character not rotating smoothly

1 messages · Page 1 of 1 (latest)

radiant crow
#

Hi i want my character to rotate smoothly i am using this code

using UnityEngine;

public class CharacterAiming : MonoBehaviour
{
    [SerializeField] private Transform crosshair;

    private void Start() 
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void LateUpdate() 
    {
        Vector3 worldAimTarget = crosshair.position;
        worldAimTarget.y = transform.position.y;

        Vector3 aimDir = (worldAimTarget -transform.position).normalized;
        Quaternion rot = Quaternion.LookRotation(aimDir);
        transform.rotation =      Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * 15f);
    }
}
#

how can i send a screen recording of it?

tall panther
#

Does it rotate suddenly

#

In 1 frame?

worn edge
# radiant crow Hi i want my character to rotate smoothly i am using this code ```cs using Unity...

you will never get a smooth/consistent movement when using lerp/slerp and having the same property be the input of lerp and the target the result is assigned to, like this
transform.rotation = Quaternion.Slerp(transform.rotation, ..
detatime or not it leads to a different speed depending on difference between the first and the second rotation parameter for slerp. maybe you could try this instead
https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html

radiant crow
radiant crow
#

i wrote this code now

#

or ig theres a problem in my code

#

using UnityEngine;

public class CharacterAiming : MonoBehaviour
{
    [SerializeField] private float turnSpeed;
    [SerializeField] private Transform crosshair;

    private void Start() 
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update() 
    {
        Vector3 worldAimTarget = crosshair.position;
        worldAimTarget.y = transform.position.y;

        Vector3 aimDir = (worldAimTarget - transform.position).normalized;
        Quaternion rot = Quaternion.LookRotation(aimDir);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed);
    }
}
#

its not rotating

#

it rotates when i move my mouse very slow

#

and when i increase my turn speed value it jitters very much

#

well i think theres a problem in my

#

see this video

worn edge
#

next time you can change the file ending to mp4 so it embeds

#

i mean, rotate towards will use the shortest path from start to target direction, so you get an effect like this, but it shouldnt change that fast, problem is that your code should already make a constant speed rotation:
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed);
so i sadly dont have more suggestions

radiant crow
#

ok

#

thanks for your help