Y
Y
Yugg02020-05-27 20:51:48
Unity
Yugg0, 2020-05-27 20:51:48

How in my case is it better to implement mechanics in Unity3D?

public class AItest : MonoBehaviour
{
    public float dis = 2;

    public Camera cam;
    public NavMeshAgent agent;

    private void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.CompareTag("Ground"))
                {
                    //Если дистанция будет больше "dis", то объект движется. 
                    //agent.SetDestination(hit.point);

                    //Если  дистанция будет меньше "dis", то объект не движется.
                }
            }
        }
    }
}


This code makes it clear how the object moves.

But we need to implement something - we need to make it clear to the object that when its distance from "hit.point" becomes less than "dis", it should stop moving.

I've been trying to find a solution for a long time, but so far I haven't been able to.
Who has ideas?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Herman Coffman, 2020-05-28
@Gera01

I did through LookAt.

if (Vector3.Distance(transform.position, player.transform.position) < seeDistance) // если видит игрока
            {
                if (canRide == true)
                {
                    transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z));
                    transform.Translate(new Vector3(0, 0, speed * Time.deltaTime)); // смотрит и двигается к игроку
                }
}

PS if it helped - like it and mark it as a solution.

C
CHIDWI, 2020-05-28
@CHIDWI

And what exactly is the problem? You have a bunch of data in hit. For example you can get its position in the hit.collider.gameobject.transform.position world and measure the distance between it and this.gameobject.transform.position via vector3 distance or just count the difference.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question