Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemy chases player after OnTriggerEnter

Discussion in 'Scripting' started by renvaar, Dec 4, 2014.

  1. renvaar

    renvaar

    Joined:
    Dec 4, 2014
    Posts:
    4
    I have a script for my 'enemy' that stops said enemy moving towards the player until the OnTriggerEnter caused by the player when it enters the collision sphere of the enemy. then i was planning to have the enemy stop chasing and return to original position when the player exits the collision sphere.. but of course... the collision sphere moves with the enemy, when i want the collision area not to move, and the enemy never to leave it... can someone tell me how to proceed? I'm a real scripting noob... so..ya know...be gentle... I won't understand anything too complicated...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI2 : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     public int moveSpeed;
    8.     public int rotationSpeed;
    9.     public int maxDistance;
    10.     public bool trigger;
    11.  
    12.     private Transform myTransform;
    13.  
    14.     void Awake(){
    15.      
    16.         myTransform = transform;
    17.      
    18.     }
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.  
    23.         moveSpeed = 1;
    24.         rotationSpeed = 1;
    25.      
    26.     }
    27.  
    28.     void  OnTriggerEnter(Collider other){
    29.  
    30.             if (other.tag == "Player"){
    31.  
    32.             GameObject go = GameObject.FindGameObjectWithTag ("Player");
    33.          
    34.             target = go.transform;
    35.          
    36.             maxDistance = 0;
    37.  
    38.         trigger = true;
    39.         }
    40.         }
    41.  
    42.     // Update is called once per frame
    43.     void Update () {
    44.  
    45.         if (trigger == true){
    46.         Debug.DrawLine (target.position, myTransform.position, Color.yellow);
    47.      
    48.         //look at target
    49.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed *Time.deltaTime);
    50.      
    51.         //move towards target
    52.         if (Vector3.Distance (target.position, myTransform.position) > maxDistance) {
    53.             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    54.  
    55.             }
    56.         }
    57.     }
    58.  
    59.  
    60. }
    61.  
     
  2. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    So, I'm going to recommend making an empty game object, creating whatever collider you want to act as the enemy's visibility radius, set it to trigger, and then just parent your enemy to that, and fit your script to the above suggestion as no matter what, if your collider is a component or parented under the enemy in question, he will move with the enemy.
     
  3. renvaar

    renvaar

    Joined:
    Dec 4, 2014
    Posts:
    4
    That's basically what I already have, but I want the area of the sphere collider not to move with the enemy, but stay in the spot I place it... so he won't chase the player out of that collider
     
  4. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    Look, it needs to be so that the collider is not parented or a component to anything, and is on it's own seperate game object, if all goes wrong, put a seperate script that just does something like: on start, set a new vector3 to the start position and every frame set the collider back to the position. Luckily, that won't be necessary if you do what I said.
     
  5. renvaar

    renvaar

    Joined:
    Dec 4, 2014
    Posts:
    4
    But if its not attached to my enemy, how to I use the OnTrigger function in my enemy script? every tutorial or example I can find shows them attached...
     
    Last edited: Dec 4, 2014
  6. LightWell

    LightWell

    Joined:
    Sep 6, 2014
    Posts:
    43
    Something like this:
    Code (JavaScript):
    1. function Start () {
    2. colliderthing = GameObject.Find("GameObject").GetComponent("BoxCollider");
    3. }
    Basically what this does i just I find the blank game object (in my test project I didn't rename it so I just found the object named thus.) and then got the boxcollider from the component, then just set that to a variable and manipulate it as you want.
     
  7. renvaar

    renvaar

    Joined:
    Dec 4, 2014
    Posts:
    4
    So it's been a while but I assume its ok to re-post as I've made progress with this but hit a bump. I've created an independent box collider with this script attached...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Trigger: MonoBehaviour {
    5.  
    6.     public GameObject Enemy;
    7.     public EnemyAI2 other;
    8.  
    9.     // Use this for initialization
    10.  
    11.     void Awake(){
    12.  
    13.         GameObject Enemy = GameObject.Find ("Enemy");
    14.         other = Enemy.GetComponent<EnemyAI2>();
    15.  
    16.         }
    17.     void Start () {
    18.  
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.  
    24.     }
    25.     void  OnTriggerEnter(Collider player){
    26.  
    27.         if (player.tag == "Player"){
    28.  
    29.             other.FindPlayer ();
    30.         }
    31.  
    32.             }
    33. }
    Which is working... but then I had to change my enemy code to this..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI2 : MonoBehaviour {
    5.  
    6.     public Transform target;
    7.     public int moveSpeed;
    8.     public int rotationSpeed;
    9.     public int maxDistance;
    10.  
    11.     private Transform myTransform;
    12.  
    13.     void Awake(){
    14.      
    15.         myTransform = transform;
    16.      
    17.     }
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.      
    22.         moveSpeed = 1;
    23.         rotationSpeed = 1;
    24.      
    25.     }
    26.  
    27.          
    28.          
    29.     public void FindPlayer (){
    30.  
    31.         GameObject go = GameObject.FindGameObjectWithTag ("Player");
    32.      
    33.         target = go.transform;
    34.      
    35.         maxDistance = 0;
    36.      
    37.         Debug.DrawLine (target.position, myTransform.position, Color.yellow);
    38.      
    39.         //look at target
    40.         myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed *Time.deltaTime);
    41.      
    42.         //move towards target
    43.         if (Vector3.Distance (target.position, myTransform.position) > maxDistance) {
    44.             myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    45.          
    46.         }
    47.  
    48.  
    49.         }
    50.      
    51.  
    52.  
    53.     // Update is called once per frame
    54.     void Update () {
    55.  
    56.  
    57.         }
    58.     }
    59.  
    60.  
    But nothing happens... when as far as i can see... this should work...

    Edit: Oh, some of that which makes the enemy move should be in update... so he continues to move... right? but when i do that i get a'the variable target of enemyAI2 has not been assigned' error...
     
    Last edited: Dec 11, 2014