Синтаксис:
Используется csharp
public LayerMask layer;
public int MaxRay;
public NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Debug.DrawRay(gameObject.transform.position, gameObject.transform.forward * MaxRay, Color.red);
if (!Physics.Raycast(gameObject.transform.position + transform.up, gameObject.transform.forward, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 1));
}
else
{
Debug.Log(hit.distance);
if (!Physics.Raycast(gameObject.transform.position + transform.up, gameObject.transform.right, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y, gameObject.transform.position.z));
}
else
{
Debug.Log(hit.distance);
if (!Physics.Raycast(gameObject.transform.position + transform.up, -gameObject.transform.right, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x - 1, gameObject.transform.position.y, gameObject.transform.position.z));
}
else
{
Debug.Log(hit.distance);
}
}
}
}
public int MaxRay;
public NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RaycastHit hit;
Debug.DrawRay(gameObject.transform.position, gameObject.transform.forward * MaxRay, Color.red);
if (!Physics.Raycast(gameObject.transform.position + transform.up, gameObject.transform.forward, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z + 1));
}
else
{
Debug.Log(hit.distance);
if (!Physics.Raycast(gameObject.transform.position + transform.up, gameObject.transform.right, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x + 1, gameObject.transform.position.y, gameObject.transform.position.z));
}
else
{
Debug.Log(hit.distance);
if (!Physics.Raycast(gameObject.transform.position + transform.up, -gameObject.transform.right, out hit, layer, MaxRay))
{
agent.SetDestination(new Vector3(gameObject.transform.position.x - 1, gameObject.transform.position.y, gameObject.transform.position.z));
}
else
{
Debug.Log(hit.distance);
}
}
}
}
а теперь в чём проблема: при выставленной MaxRay на 1 луч уходит на расстояние в 20 с лишним. вопрос - что я делаю не так?