The droid by default is in the air floating in the air. I added a waypoints script and the droid is moving between the waypoints. The problem is when the player when I move the player first person and standing in the middle of the droid path. Then when the droid is getting closer and then collide with the player he get pushed like physics and sometimes he stuck in some corner between walls and never continue.
The player have this components : Character Controller , Rigidbody.
The player settings : Rigidbody : Use Gravity an Is Kinematic both enabled true.
The npc(droid) have Animaitr, Box Collider, Rigidbody.
The npc(droid) settings : Rigidbody : Only Use Gravity is enabled true.
I’m not sure if using navmesh agent on the droid is possible since he is floating and moving in the air not on the floor/surface.
The script I’m using to move the npc(droid) between the waypoints :
The script is attached to empty gameobject :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public GameObject objectToMove;
public GameObject[] waypoints;
public Transform target;
public float moveSpeed = 1f;
public float rotationSpeed = 1f;
private Transform myTransform;
private int targetsIndex = 0;
private Vector3 originalPosition;
// Use this for initialization
void Start()
{
if (objectToMove == null)
{
myTransform = transform;
}
else
{
myTransform = objectToMove.transform;
}
originalPosition = myTransform.position;
if (waypoints.Length == 0)
{
waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
}
}
// Update is called once per frame
void Update()
{
WayPointsAI();
DrawLinesInScene();
}
private void WayPointsAI()
{
if (targetsIndex == waypoints.Length)
targetsIndex = 0;
target = waypoints[targetsIndex].transform;
float distance = Vector3.Distance(myTransform.position, target.transform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
if (distance < 0.3f)
targetsIndex++;
}
void DrawLinesInScene()
{
// draw lines between each checkpoint //
for (int i = 0; i < waypoints.Length - 1; i++)
{
Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue);
}
// draw a line between the original transform start position
// and the current transform position //
Debug.DrawLine(originalPosition, myTransform.position, Color.red);
// draw a line between current transform position and the next waypoint target
// each time reached a waypoint.
if (target != null)
Debug.DrawLine(target.transform.position, myTransform.position, Color.green);
}
The reason both player and npc have rigidbody and colliders is because I need them to be able to walk through doors that open when the player/npc enter the door/s area and trigger the door/s to open.
Later on not only the player but other npc’s will walk around and also I want them to avoid the droid or the droid to avoid them and keep moving his path. Somehow to find how to move around and get back to the path so the droid will keep moving between the waypoints.
I’m not sure if navmesh agent is a solution for the droid ?