Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Return spawn point when out of range

Discussion in 'Scripting' started by Paykoman, Oct 25, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i hv this beginig ai script and it works perfectly i just wonder how can i change it to when i go out out enemy range it return to initial position and not stay in position when it stop follow me...

    thats the script...

    ty

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour
    5. {
    6.     float distance;
    7.     float lookAtDistance = 25.0f;
    8.     float attackRange = 15.0f;
    9.     float moveSpeed = 5.0f;
    10.     float damping = 6.0f;
    11.  
    12.     public Transform target;
    13.    
    14.     void Update()
    15.     {
    16.         distance = Vector3.Distance(target.position, transform.position);
    17.        
    18.         if(distance < lookAtDistance)
    19.         {
    20.             renderer.material.color = Color.yellow;
    21.             LookAt();
    22.         }
    23.        
    24.         if(distance > lookAtDistance)
    25.         {
    26.             renderer.material.color = Color.green;
    27.         }
    28.        
    29.         if (distance < attackRange)
    30.         {
    31.             renderer.material.color = Color.red;
    32.             AttackPlayer();
    33.         }
    34.     }
    35.    
    36.     void LookAt()
    37.     {
    38.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    39.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    40.     }
    41.    
    42.     void AttackPlayer()
    43.     {
    44.         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    45.     }
    46. }
    47.  
    48.  
    49.  
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500

    if helps the explanation i want to know how do i make to back to initial position when it turn yellow to green...
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Something like this:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         distance = Vector3.Distance(target.position, transform.position);
    4.      
    5.         if(distance < lookAtDistance)
    6.         {
    7.             renderer.material.color = Color.yellow;
    8.             LookAt();
    9.         }
    10.      
    11.         if(distance > lookAtDistance)
    12.         {
    13.             if(renderer.material.color == Color.Yellow) {
    14.                  ReturnToInitialPosition();
    15.             }
    16.             renderer.material.color = Color.green;
    17.         }
    18.      
    19.         if (distance < attackRange)
    20.         {
    21.             renderer.material.color = Color.red;
    22.             AttackPlayer();
    23.         }
    24.     }
    But instead of colors you should probably use an enum instead :'P
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The colors was only to test i already change to a game charater :) i will test this.. Ty for tje support
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok i test but i think i dont get it right. Can u plz help me with that... my corrent code is that and im not figure in out how can i turn the enemy back to inicial position when player go out of his attackRange...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour
    5. {
    6.     float distance;
    7.     float lookAtDistance = 25.0f;
    8.     float attackRange = 15.0f;
    9.     float damping = 6.0f;
    10.  
    11.     public int speed;
    12.  
    13.     public AnimationClip idle;
    14.     public AnimationClip run;
    15.  
    16.     public Transform target;
    17.    
    18.     void Update()
    19.     {
    20.         distance = Vector3.Distance(target.position, transform.position);
    21.        
    22.         if (distance < lookAtDistance)
    23.         {
    24.             animation.CrossFade (idle.name);
    25.             LookAt ();
    26.         }
    27.         else
    28.         {
    29.                
    30.         }
    31.        
    32.         if (distance < attackRange)
    33.         {
    34.             animation.CrossFade(run.name);
    35.             AttackPlayer();
    36.         }
    37.  
    38.     }
    39.    
    40.     void LookAt()
    41.     {
    42.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    43.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    44.     }
    45.    
    46.     void AttackPlayer()
    47.     {
    48.         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    49.     }
    50. }
    51.  
    52.  
    53.  
     
  6. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Create a boolean called "isReturning".
    now in the update function you'll run the usual code if he isn't returning, BUT if u want it to return, set that boolean to true. And so you'll make the AI move to the initial position. when the player reaches the initial position you can change the boolean to false again. u can store the initial position in the start function:
    Code (CSharp):
    1. private Vector3 initialPosition;
    2.  
    3. void Start() {
    4.      initialPosition = transform.position;
    5. }
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi Magichiian thx for ur help. I kinda understand wt u are trying to teach me but i cant put them to work i try the code above but seems something is missing me and the enemy dont return to initial position... can u plz give a hand again :s
    appreciate ut help.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour
    5. {
    6.     float distance;
    7.     float lookAtDistance = 25.0f;
    8.     float attackRange = 15.0f;
    9.     float damping = 6.0f;
    10.  
    11.     public int speed;
    12.  
    13.     bool isReturning;
    14.  
    15.     public AnimationClip idle;
    16.     public AnimationClip run;
    17.  
    18.     public Transform target;
    19.  
    20.     private Vector3 initialPosition;
    21.  
    22.     void start()
    23.     {
    24.         initialPosition = transform.position;
    25.         isReturning = false;
    26.     }
    27.    
    28.     void Update()
    29.     {
    30.         distance = Vector3.Distance(target.position, transform.position);
    31.        
    32.         if (distance < lookAtDistance)
    33.         {
    34.             animation.CrossFade (idle.name);
    35.             LookAt ();
    36.         }
    37.         else
    38.         {
    39.  
    40.         }
    41.  
    42.         if (distance > lookAtDistance && distance>attackRange)
    43.         {
    44.             isReturning = true;
    45.         }
    46.                
    47.         if (distance < attackRange)
    48.         {
    49.             animation.CrossFade(run.name);
    50.             AttackPlayer();
    51.         }
    52.  
    53.     }
    54.    
    55.     void LookAt()
    56.     {
    57.         Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
    58.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    59.     }
    60.    
    61.     void AttackPlayer()
    62.     {
    63.         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    64.     }
    65. }
    66.  
    67.  
     
  8. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class EnemyAI : MonoBehaviour
    4. {
    5.     float distance;
    6.     float lookAtDistance = 25.0f;
    7.     float attackRange = 15.0f;
    8.     float damping = 6.0f;
    9.     public int speed;
    10.     bool isReturning;
    11.     public AnimationClip idle;
    12.     public AnimationClip run;
    13.     public Transform target;
    14.     private Vector3 initialPosition;
    15.  
    16.     void start()
    17.     {
    18.         initialPosition = transform.position;
    19.         isReturning = false;
    20.     }
    21.     void Update()
    22.     {
    23.         if (!isReturning)
    24.         {
    25.             distance = Vector3.Distance(target.position, transform.position);
    26.          
    27.             if (distance < lookAtDistance)
    28.             {
    29.                 animation.CrossFade (idle.name);
    30.                 LookAt (target.position);
    31.             }
    32.             else //LookDistance is always bigger than the attackRange so i commented it out. //if (distance > lookAtDistance) && distance>attackRange)
    33.             {
    34.                 isReturning = true;
    35.             }
    36.                  
    37.             if (distance < attackRange)
    38.             {
    39.                 animation.CrossFade(run.name);
    40.                 MovePlayerForward();
    41.             }
    42.         }
    43.         else
    44.         {
    45.             LookAt (initialPosition);
    46.             MovePlayerForward();
    47.            
    48.             distance = Vector3.Distance(initialPosition, transform.position);
    49.             if (distance < 20)
    50.             {
    51.                 isReturning = false;
    52.             }
    53.         }
    54.     }
    55.     void LookAt(Vector3 targetPosition)
    56.     {
    57.         Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
    58.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    59.     }
    60.    
    61.     void MovePlayerForward()
    62.     {
    63.         transform.Translate(Vector3.forward * speed * Time.deltaTime);
    64.     }
    65. }
     
  9. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Create a variable of type Vector3 called OriginalPosition, at Start() set it to this.Transform.Position;

    Later on say Transform.Position = OriginalPosition
     
  10. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @Paykoman
    By the way, why aren't you using navmesh to make it follow the player?

    @Misterselmo
    I think he wanted it to walk back, not teleport o.o
     
  11. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yes i want it to run back to initial position... Good point i forget the navmesh but i need learn use it propertly... So can u plz tell me wt im doing wrong with this method? i will try later the navmesh one... Appreciate all the help
     
  12. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Nvm u al
    ready answer me :)
     
    Magiichan likes this.
  13. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Same thing, except set target position to OriginalPosition.
     
  14. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    There is no target position though :'P
     
  15. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    Well maybe that's your problem.
     
  16. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Mostly the problem of @Paykoman, since he has a very limited knowledge on programming.
     
  17. RJ-MacReady

    RJ-MacReady

    Joined:
    Jun 14, 2013
    Posts:
    1,718
    We all have very limited knowledge of programming. This is not understanding the problem.
     
  18. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Instead of arguing with me, how about you give him a bit more elaborate advice on how to achieve the AI to walk back...
    xD
     
  19. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    :) y lol i appreciate if