What i’m doing is:
-
When the game is starting my player is Idle. And i can move the mouse cursour around and the player will rotate and face the mouse position.
-
When i click one click somewhere on the ground with the mouse the player will start walking to this direction.
-
When the player get to the mouse clicked position he should Idle again like when i was running the game first time. But this is the problem:
When the player is getting to the clicked mouse position he start rotating like crazy. Only if i click double mouse click he will idle and stop rotating. I’m not 100% sure if i make mouse double left button click make it idle again not sure what is going on.
But what i want is one click make the player start walking to this mouse position when he get to this position Idle the player and let the user move the mouse around again so the player will rotate according to the mouse cursour position.
This is my script: With the _animator i make the player to Walk(and move) or to Idle.
using UnityEngine;
using System.Collections;
public class MoveObjects : MonoBehaviour
{
private Animator _animator;
// Use this for initialization
void Start()
{
isWalking = true;
_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)); //the number here is compltely arbitrary
}
if (Input.GetMouseButtonDown(0))
{
if ((transform.position - hit.point).magnitude < 1.0f)
{
_animator.CrossFade("Idle", 0);
}
else
{
_animator.CrossFade("Walk", 0);
}
}
}
}