For my class i am making an enemy in a game based off roll a ball. I have added walls and they all work fine except when i make the enemy a trigger for the players death. Whenever he is a trigger he kills the player and return you to the main menu but he can go through the walls and when he is not he doesnt go through walls but cannot kill the player. is there another way i have to do this or can i add something. here is the script i have so far
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class aiEasy : MonoBehaviour {
public float fpsTargetDistance;
public float enemyLookDistance;
public float attackDistance;
public float enemyMovementSpeed;
public float damping;
public Transform fpsTarget;
Rigidbody theRigidbody;
Renderer myRenderer;
public Boundary boundary;
void Start ()
{
myRenderer = GetComponent<Renderer> ();
theRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate ()
{
fpsTargetDistance = Vector3.Distance (fpsTarget.position, transform.position);
if (fpsTargetDistance < enemyLookDistance)
{
myRenderer.material.color = Color.yellow;
lookAtPlayer ();
}
if (fpsTargetDistance < attackDistance) {
myRenderer.material.color = Color.red;
attackPlease ();
}
else
{
myRenderer.material.color = Color.blue;
}
theRigidbody.position = new Vector3
(
Mathf.Clamp (theRigidbody.position.x, boundary.xMin, boundary.xMax),
0.5f,
Mathf.Clamp (theRigidbody.position.z, boundary.zMin, boundary.zMax)
);
}
void lookAtPlayer()
{
Quaternion rotation = Quaternion.LookRotation (fpsTarget.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * damping);
}
void attackPlease()
{
theRigidbody.AddForce (transform.forward * enemyMovementSpeed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Player"))
{
Application.LoadLevelAdditive ("MainMenu");//code for death barrier
}
}
}