- Home /
How can i avoid mouse double click on second time to make my player idle ?
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);
}
}
}
}
Answer by b1gry4n · Oct 24, 2016 at 04:15 AM
You need a distance check for the target to say if the object reached it or not. It will constantly try to "lookat" that position right now, which is causing the crazy rotation. Your hit.point is only updated if a valid surface for the mouse is hovered over. If the mouse isnt, you are feeding in a ray as the target. The target he is looking at is the ray position, but hes trying to move to the hit.point (which is no longer a valid position since the mouse moved) Store the target the object is walking towards, check if the distance falls below a threshold, then transition back to idle state if the target is reached.
As youve noticed, things are going to only get more complex from here. I suggest you implement a state machine that way you can easily switch between these states (idle, move, etc). I suggest you dont combine the mouse hit check with the movement of the object. Distinguish them as 2 different methods. The mouse target info is fed into the state machine.
public float targetReachedDistance = 0.1f;
if (target != null)
{ //move the player to the target
if (Vector3.Distance(transform.position, target.position) <= targetReachedDistance)
{
//we reached the destination
target = null;
}
}
else
{
//idle
}
Its ok to have more than 1 script. Dont try to do too many different things inside of 1 script. At this point, personally, I would have split up your script into 3 different scripts.
PlayerController, which controls the movement and states of the player
MouseTargetController, which feeds the mouse target info into the player controller
AnimationController, which reads the player controllers state and updates the animation based off that
Your answer
Follow this Question
Related Questions
Why i'm getting UnassignedReferenceException when running the game ? 1 Answer
How can i make the spaceship to land/take off with physics vertical ? 2 Answers
To pass a variable between scenes should I use scriptableobject or static ? 1 Answer
How can i lock the mouse cursor in the middle of the screen ? 1 Answer