Hi,
I am trying to create an enemy that hovers and follows the player around… like a Cacodemon from Doom/hovering drone would. I found a demo on Youtube, and while it works incredibly well, it fails in a huge way when it comes to detecting large colliders like terrain. It just seems to ignore them and pass through them. I have double checked tags, layers, and the physics matrix… all is good there. A comment in the Youtube demo stated that if two of the rays hit the object at the same time it would cause issues. I’ll paste my code below. Any help would be a huge help. I’ve been stuck with this for a week now.
To try/test the code create 2 cubes, name one player and one enemy. Set the player as the target for the enemy. Create a bunch of small cubes with colliders 1-2 units in scale. Place them around like walls and leave some floating in the air. Press the play button. Select the player and drag him around in the viewport. The variables that are exposed in the inspector will probably need to be tweaked a bit to.
I’m also wondering if anyone has run into a good thread here for creating an enemy like a drone that will chase the player around objects, through halls, and over things. If so a link to it would be awesome.
Thanks,
-Kory
public class EnemyHoverFollow : MonoBehaviour {
[SerializeField]Transform target;
[SerializeField]float movementSpeed = 10.0f;
[SerializeField]float rotationalDamp = 0.5f;
[SerializeField]float rayCastOffset = 2.5f;
[SerializeField]float detectionDistance = 20.0f;
void Start(){
target = GameObject.Find("Player").transform;
}
void Update(){
Pathfinding ();
Move ();
}
void Turn(){
Vector3 pos = target.position - transform.position;
Quaternion rotation = Quaternion.LookRotation (pos);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, rotationalDamp * Time.deltaTime);
}
void Move(){
transform.position += transform.forward * movementSpeed * Time.deltaTime;
}
void Pathfinding(){
RaycastHit hit;
Vector3 raycastOffset = Vector3.zero;
Vector3 left = transform.position - transform.right * rayCastOffset;
Vector3 right = transform.position + transform.right * rayCastOffset;
Vector3 up = transform.position + transform.up * rayCastOffset;
Vector3 down = transform.position - transform.up * rayCastOffset;
Debug.DrawRay (left, transform.forward * detectionDistance, Color.cyan);
Debug.DrawRay (right, transform.forward * detectionDistance, Color.cyan);
Debug.DrawRay (up, transform.forward * detectionDistance, Color.cyan);
Debug.DrawRay (down, transform.forward * detectionDistance, Color.cyan);
//Right and Left testing
if(Physics.Raycast(left, transform.forward, out hit, detectionDistance)){
raycastOffset += Vector3.right;
}
else if(Physics.Raycast(right, transform.forward, out hit, detectionDistance)){
raycastOffset -= Vector3.right;
}
//Up and Down testing
if(Physics.Raycast(up, transform.forward, out hit, detectionDistance)){
raycastOffset -= Vector3.up;
}
else if(Physics.Raycast(down, transform.forward, out hit, detectionDistance)){
raycastOffset += Vector3.up;
}
if (raycastOffset != Vector3.zero) {
transform.Rotate (raycastOffset * 5.0f * Time.deltaTime);
} else {
Turn ();
}
}
}