Answer the question
In order to leave comments, you need to log in
How to determine the convergence of the coordinates of the mouse click and the object?
There is a part of the code that turns on the walking animation when the object starts moving. I tried to compare the coordinates of the object and the click, but it doesn't work. Can someone suggest what is wrong?
public class Player : MonoBehavior
{
public LayerMask WhatCanBeClickedOn;
private NavMeshAgent myAgent;
public Animator anim;
private Vector3 MousePos;
public Transform player;
void Start()
{
myAgent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
player = GetComponent<Transform>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray myRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
MousePos = Input.mousePosition;
if (Physics.Raycast(myRay, out hitInfo, 35, WhatCanBeClickedOn))
{
myAgent.SetDestination(hitInfo.point);
anim.SetBool("Walk", true);
}
if (MousePos == player.transform.position)
{
anim.SetBool("Walk", false);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question