Answer the question
In order to leave comments, you need to log in
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", то объект не движется.
}
}
}
}
}
Answer the question
In order to leave comments, you need to log in
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)); // смотрит и двигается к игроку
}
}
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 questionAsk a Question
731 491 924 answers to any question