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

Return to original spot (spawn spot)

Discussion in 'Getting Started' started by Loff, Jan 21, 2015.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hey!
    (2D, C#, 2Dbox collider)
    Working on a enemy AI , pretty new on that part by the way. I'm Trying to make the enemy go back to his original spot after chasing the player.

    So at the start I save his spawn point with
    Code (CSharp):
    1. void Start () {
    2.         spot = gameObject.transform.position;
    3.     }
    And in the update I make him follow the player if he is within distance. However I have tried lots of things, but can't make him move back to original spot. IF player is out of distance OR enemy is outside his max range.
    I have tried to make him walk back, but I only succeed to make him teleport back or by going to a hidden object, and I don't want those two options.

    Code (CSharp):
    1. void Update () {
    2.            
    3.             find_player = transform.Find("/Player");
    4.             dist = Vector2.Distance(find_player.position, transform.position);
    5.  
    6.             if (dist < 4) {
    7.        
    8.                         if (find_player.position.x < myTransform.position.x) {
    9.                                 myTransform.position -= myTransform.right * speed; // player is left of enemy, move left
    10.                         } else if (find_player.position.x > myTransform.position.x) {
    11.                                 myTransform.position += myTransform.right * speed; // player is right of enemy, move right
    12.                         }
    13.                 } else {
    14. //Go back to spawn spot..
    15.                 }
    16.            
    17.    
    18.     }
    My attempts so far have only made him teleport, move like crazy and such so I would love to hear any suggestions.
    Maybe jump would be cool as well, but I like to start with basic :)

    thanks!
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Why not reuse your code that goes towards the player, but make the spot the point you move towards. Something like this:
    Code (CSharp):
    1. void Update () {
    2.          
    3.             find_player = transform.Find("/Player");
    4.             dist = Vector2.Distance(find_player.position, transform.position);
    5.             if (dist < 4) {      
    6.                         if (find_player.position.x < myTransform.position.x) {
    7.                                 myTransform.position -= myTransform.right * speed; // player is left of enemy, move left
    8.                         } else if (find_player.position.x > myTransform.position.x) {
    9.                                 myTransform.position += myTransform.right * speed; // player is right of enemy, move right
    10.                         }
    11.                 } else {
    12.                         if (spot.x < myTransform.position.x) {
    13.                                 myTransform.position -= myTransform.right * speed; // player is left of enemy, move left
    14.                         } else if (spot.x > myTransform.position.x) {
    15.                                 myTransform.position += myTransform.right * speed; // player is right of enemy, move right
    16.                 }
    17.          
    18.  
    19.     }
     
    Loff likes this.
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hmm... that's a great question :) Works great, thank you.
     
  4. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
  5. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    I tried that, but that means I need a object rather then using it start pos? And I didn't wanna spawn a new obj just so it can target that. I might do it wrong tho :p
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Actually, MoveTowards doesn't take a GameOject as input. It takes vectors containing the positions:

    Code (CSharp):
    1. public static Vector2 MoveTowards(Vector2 current, Vector2 target, float maxDistanceDelta);
     
  7. DustyMcp

    DustyMcp

    Joined:
    May 23, 2013
    Posts:
    25
    Sorry meant gameobject.transform.position =)