R
R
ROWASTAIR2021-04-21 12:20:01
Unity
ROWASTAIR, 2021-04-21 12:20:01

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

1 answer(s)
K
kokapuk, 2021-04-21
@kokapuk

https://answers.unity.com/questions/569376/how-to-... are you talking about this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question