I need to make realistic human movement (3D) using mouse click.
- Get mouse click point using
Raycast.
- Smoothly
Slerp to LookRotation.
- Move
transform.forward.
Everything works fine, except I have problem with circular movement (close radius).
- If dont move
transform.forward while Slerp to LookRotation or MoveTowards instead of transform.forward it will be spinning like whirligig on circular movement.
- If do move
transform.forward while while Slerp to LookRotation it will distort trajectory of straight direct movement.
Is there any solution to move circular realistic (not spin like whirligig) and move straight direct when needed (not distort trajectory)?
if (Input.GetMouseButtonDown(1))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit[] hits = Physics.RaycastAll(ray);
foreach (RaycastHit hit in hits)
{
if (hit.transform.tag == "Ground")
{
DestinationPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
DestinationRotation = Quaternion.LookRotation(DestinationPosition - transform.position);
break;
}
}
}
if (Vector3.Distance(transform.position, DestinationPosition) > 1)
{
transform.rotation = Quaternion.Slerp(transform.rotation, DestinationRotation, RotationSpeed * Time.deltaTime);
transform.position += transform.forward * MovementSpeed * Time.deltaTime;
//transform.position = Vector3.MoveTowards(transform.position, DestinationPosition, MovementSpeed * Time.deltaTime);
animation.CrossFade("run");
}
else
{
animation.CrossFade("idle");
}