Search Unity

Moving and following an object

Discussion in 'Scripting' started by Rincewind, Jul 22, 2006.

  1. Rincewind

    Rincewind

    Joined:
    Apr 5, 2006
    Posts:
    22
    I have this Commando-kind of top down shooter in the works. I have two game objects: player and one enemy. When player goes beyond < -30 along the Z-axis, enemy moves between z -25.0 - -20.0. After that it should stay put and just start rotating, where ever the player moves, but now enemy lunges back to -25.0 and starts sideways along the X-axis :?

    Here's the code:
    Any suggestions?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Do you want to make it so that the enemy starts walking towards you when he is close to the player and otherwise does some kind of patroling along that line?
     
  3. Rincewind

    Rincewind

    Joined:
    Apr 5, 2006
    Posts:
    22
    No. He should move that distance (guy running to his post and then following the player) and nothing else. I've planned only having stationary or enemies, who move a little to keep it simple for now.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. var player = gameObject.Find("Player");
    3. if (player.transform.position.z < -30) {
    4. var speed = Time.deltaTime * 5;
    5. transform.Translate(0, 0, speed);
    6. var enemy = GameObject.Find("Enemy");
    7. transform.Translate(0, 0, speed);
    8. enemy.transform.position.z = Mathf.Clamp(enemy.transform.position.z, -25.0,-20.0);
    9. if (Mathf.Approximately(enemy.transform.position.z, -20)) {
    10. transform.LookAt(target);
    11. }
    12.  
    YOu probalby just need to move all your transform.Translate calls int an else block after the
    if (Mathf.Approximately(enemy.transform.position.z, -20)) {
     
  5. Rincewind

    Rincewind

    Joined:
    Apr 5, 2006
    Posts:
    22
    So I've edited this a couple of dozen times, asked some help and here's where I've come. However, Unity doesn't like, when I call a function when declaring a variable. How would I go tackling this?

    Code (csharp):
    1. var target : Transform;
    2. var speed = 5;
    3.  
    4. function Update()
    5. {
    6.  
    7. var player = gameObject.Find("Player");
    8.     if (player.transform.position.z < -30)
    9. {
    10.         var enemy = gameObject.Find("Enemy");
    11.         transform.Translate(0, 0, speed);
    12.         enemy.transform.position.z = Mathf.Clamp(enemy.transform.position.z, -25.0,-20.0);
    13. }
    14. else if (Mathf.Approximately(enemy.transform.position.z, -20))
    15. {
    16.     transform.LookAt(target);
    17. }
    18.  
    19. }
    [/code]
     
  6. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I shouldn't answer if I'm not 100% certain this will work, but too bad for you, I will anyway :D

    Try changing var player = gameObject.Find("Player"); to just player = gameObject.Find("Player"); and adding this at the top: var player : GameObject;

    So you're not declaring it AND using the function at the same time.

    You could also use private var player : GameObject; if you didn't want the var cluttering up the Inspector window.
     
  7. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    What message is Unity giving you? Also, does "Player" change. If not, why not load it in Start()?