Question by
AssEMxyz · Feb 08, 2016 at 10:09 AM ·
scripting problem3daimovment
Problem: trigger within object
Hello! I'm new to programming, so I searched a lot, and I tried to modify some scripts, but all failed. It has been a week. But I didn't find the answer.
The problem is: I have a cone. This cone is a child to the player. It's likely a flash light and I need an enemy to run away if it is trapped in the cone (the light), but after it leaves light spot, I want it to go after me again. I don't know exactly what to do. Here is the script:
public class EnemyMovement : MonoBehaviour
{
Transform player;
NavMeshAgent nav;
public float fieldOfViewAngle = 45f;
public bool FlashLight;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
nav.SetDestination (player.position);
}
void OnTriggerStay ()
{
}
void OnTriggerExit ()
{
}
}
Thanks a lot
Comment
Answer by VisionDeep · Feb 15, 2016 at 06:12 PM
Try this unntested scripts:
public class enemyController : MonoBehaviour
{
Transform player;
public bool flashLight = false;
private float speed = 0.5f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
transform.LookAt(player);
if(flashLight)
{
transform.position += transform.forward*speed*Time.deltaTime*-1;
}
else
{
transform.position += new Vector3(player.position.x, player.position.y, player.position.z);
}
}
}
and in the playerController script:
public class playerController : MonoBehaviour
{
enemyController control = new enemyController();
public Light point;
Ray ray;
RaycastHit hit;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(Physics.Raycast(ray, out hit))
{
if(hit.transform.tag == "Enemy")
{
control.flashLight = true;
}
}
}
}