Hi, everyone. i’m trying to use vector3.lerp() to rotate the object. if the rotate speed is setting as 10f, why can’t i get same smooth turning. it is turned very slow.
and if the rotatespeed is increase to 100f, when the object’s rotation is happened, the object’s diretion is hunting between left and right.
Paste in your Unit.cs script and we’ll have a look. Be sure to paste the text and not a screenshot.
- Forum User Guides : How to apply code formatting within your post
using UnityEngine;
public class Unit : MonoBehaviour
{
private Vector3 targetPosition;//declare the targetposition;
[SerializeField] private Animator unitAnimator;//declare an animation
[SerializeField] private float moveSpeed = 4f;//declare the movespeed
[SerializeField] private float rotateSpeed = 10f;//decalre the rotatespeed
private void Awake()
{
targetPosition = transform.position;//initialize the start postion;
}
private void Update()//update method
{
float stoppingDistance = 0.1f;//declare the stopping distance between object position and target postion
if (Vector3.Distance(targetPosition, transform.position) > stoppingDistance)
{
//Debug.Log("true");
//if the distance from target postion to object positon is greater than the stoppdistance, keep moving;
//if the distance from target postion to object positon is less than the stoppdistance, stop if code excuting;
Vector3 moveDirection = targetPosition - transform.position;//calculate the move direction from object postion to target postion;
//Debug.Log("movedirection" + moveDirection);
moveDirection.Normalize();//calculate the direction to magnutide of 1 (0-1);
//Debug.Log(moveDirection);
transform.position += moveDirection * moveSpeed * Time.deltaTime;//calculate the object postion each frame and assign new position to object position
transform.forward = Vector3.Lerp(transform.position, moveDirection, rotateSpeed * Time.deltaTime); //turning is not good, rotatespeed is small, turned too slow; rotatespeed is big, the hunting is happed during turning.
// transform.LookAt(targetPosition);//always toward to target.
unitAnimator.SetBool("IsWalking", true);//set animation from idle to walking
}
else
{
unitAnimator.SetBool("IsWalking", false);//set animation from walking to idle
}
}
public void Move(Vector3 _targetPosition)
{
this.targetPosition = _targetPosition;//assign the _targetPostion to targeposition
}
}//class
thank you very much
3rd parameter of Vector3.Lerp accepts values between 0. and 1.
You need a way to calculate current ratio of travelled distance, meaning your current forward to move direction ratio.
But what I would do, is using Quaternion.RotateTowards
It takes current rotation, target rotation and rotation speed in degrees per second, like this:
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, step);
Where for target.rotation you can use :
Quaternion.LookRotation(direction)
where
Vector3 direction = targetPosition - transform.position;
And
step = rotationSpeed * Time.deltaTime
Check this out:
and this:
thanks a lot
You’re lerping using the transform.position instead of the transform.forward.
transform.forward = Vector3.Lerp(transform.forward, moveDirection, rotateSpeed * Time.deltaTime); //** Edit: forgot Time.deltaTime to keep frame independent
hello , yes it is working after remove the time.deltatime. i’m confusing about tiem.deltatime. is there any difference?
thank you very much!
Actually, you should keep the deltaTime, as it keeps the rotation frame independent, I just failed to type it in when I rewrote the method.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.