I was advised to use State Machine seen in this link the first answer:
But I'm not sure if I need that for now. My problem should be easier to solve I think, and I also don't understand how to use the State Machine.
The first part, using the RaycastHit
and the LookAt
is currently rotating and facing the mouse position whenever I move my mouse around the character. This part should be my default idle state when I'm running the game where each time the player is walking and reaching the target point (mouse clicked position).
To clarify, the other two states are Idle
and Walking
; both are animation states using HumanoidWalk
and HumanoidIdle
. When I click the mouse button, the player will "Walk" toward the target; the clicked mouse position and when it has reached there it will enter the Idle
state. Also it will return to the LookAt
mode again so that I will be able to move the mouse around and the character will rotate/face the mouse position.
However, what I'm getting currently is when the character has reached the mouse-clicked point, he begins to rotate nonstop. If I move the mouse around, the player will start walking and following the mouse movement. This is not what I want.
I tried to change the distance from 1.0f
, 2.0f
and 3.0f
for testing, but it didn't solve any of the problems.
The main Idle
state when running the game and when the character is reaching the mouse-clicked position should be somehow combine with this part:
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.name != "ThirdPersonController")
{
transform.LookAt(hit.point);
}
else
{
transform.LookAt(ray.GetPoint(100));
}
With the Idle state:
_animator.CrossFade("Idle", 0);
This is the script:
using UnityEngine;
using System.Collections;
public class MoveObjects : MonoBehaviour {
private Animator _animator;
void Start()
{
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
}
void Update()
{
MovePlayerWithMouse();
}
private void MovePlayerWithMouse()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.name != "ThirdPersonController")
{
transform.LookAt(hit.point);
}
else
{
transform.LookAt(ray.GetPoint(100));
}
if (Input.GetMouseButtonDown(0))
{
if ((transform.position - hit.point).magnitude < 1.0f)
{
_animator.CrossFade("Idle", 0);
}
else
{
_animator.CrossFade("Walk", 0);
}
}
}