How to make the agent reach the nearest "area of an object"
Hi everyone I'm new to this world and i cant figure out how I can make an AI not reach the center of the object I selected on the script but his area. In particular my AIs should get the bottom of the screen with the shortest way (not the center of the bottom screen). With transform i can use 3 coordinates (a point), i need the AIs go to a line, not to a point. This is my simple script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Enemy : MonoBehaviour
{
public Transform Player;
NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
agent.SetDestination(Player.position);
}
}
Thanks for your time
Hello.
It's not clear what are you asking for...
Can you make a simple Paint draw to understand your problem?
Can you post what you tried? what you want to get?
Thanks.
@tormentoarmagedoom Sure: $$anonymous$$y agent by default follows the red line to reach the red point (that is the center of the object) with the shortest path . However what it is supposed to do is to reach the green zone with the shortest path, not necessarily the center. So i need the destination of the agent be the green zone, not the only red point. I made myself clear?
Answer by tormentoarmagedoom · Jun 06, 2019 at 02:47 PM
Hello.
Ok I see now your problem :D
Well, I don't know if there is some function to calculate the shortest way to any point of a given mesh. IF it exists, is what you want.
But, I always try to solve the problems with creativity. In this case, i would make the Arrival zone, have multiple empty object chiilds called for example "Destination1", "destination 2"...., in all the lenght of the Arrival zone.
Then, You need to calculate each frame in Update (or every second or something like this with a corutine) what is the closest Destination to the moving object, using the function Vector3.Distance(). This way, it will move to the closest destination from its actual position.
As more Destinations childs in arrival zone, more accurate the path.
Bye!
Answer by Redef_ · Jun 06, 2019 at 07:37 PM
Hi @tormentoarmagedoom and thanks for Your time! I thought about this method too but I thought It was too heavy for the game. The script does not seem so simple, im a noob :'(
Your answer
Follow this Question
Related Questions
Navmesh, help setting position to nearest tag? (Javacript) 0 Answers
NavMesh Agent changing Model Rotation 2 Answers
Are nav mesh agents not supposed to find the path when getting stuck in other nav mesh agents ? 1 Answer
How do I make an animated AI enemy float but still move using NavMesh? 2 Answers