How would I implement obstacle avoidance in this script? I am clueless.
Hello everyone, I am working on a follow script I found in these "forums'. Here is the script:
using UnityEngine;
public class ShipFollow : MonoBehaviour
{
public Transform Player;
public int MoveSpeed = 4;
public int MaxDist = 10;
public int MinDist = 5;
void Update()
{
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
//Here Call any function U want Like Shoot at here or something
}
}
}
}
So, I have a spaceship that is following the player aiming a spotlight at them. Although I notice the ship tends to try and fly straight through the platform that the player is on. How can I make the ship avoid the ground? I have thought maybe using tags or creating an array where I drag all of the floor objects into the array and all objects in the array are to have a minimum distance from the ship. Although I have no idea how to do this, I am still fairly new to C# so any help would be gladly appreciated. Right now I am looking into maybe using the NavMesh Obstacle components, but I doubt that would work with this custom script. The ship has a mesh collider.
Answer by canslp · Jul 28, 2020 at 03:21 AM
do you want the ships to collide with the obstacles? or are you strictly trying to make a pathfinding script to get them to go around them