Search Unity

Scripting NPC's back and forth

Discussion in 'Scripting' started by LibduP, Aug 30, 2012.

  1. LibduP

    LibduP

    Joined:
    Aug 30, 2012
    Posts:
    8
    Hi everyone,

    I am currently learning the scripting in Javascript on Unity3D, so I'm trying to make an easy 2D platformer.
    I have an NPC I would like to make moving back and forth on a portion of my platform.
    The game is actually taking place on the Z axis.

    I easily made my character moving from his first place to a second point, named Point B (position: 0, -9, -18), and stop. His first place is supposed to be Point A (position: 0, -9, -8).
    Here is what I have done so far:
    Code (csharp):
    1. var speedNPCtoB : float = -0.1;
    2. private var pointA = -8;
    3. private var pointB = -18;
    4.  
    5. function Update ()
    6. {
    7.     if (this.transform.position.z > pointB)
    8.         this.transform.position.z += speedNPCtoB;
    9. }
    I just want him to move from Point A to Point Band going back.

    Anyone have any idea about how to do this?
     
  2. foxter888

    foxter888

    Joined:
    May 3, 2010
    Posts:
    530
    Vector3.Lerp
     
  3. LibduP

    LibduP

    Joined:
    Aug 30, 2012
    Posts:
    8
    Thank you for your quick reply. Now my NPC is moving from point A to point B. That's great, a good start, thank you. Here is what I have now:
    Code (csharp):
    1. var pointA : Transform;
    2. var pointB : Transform;
    3. var speedNPC : float = 0.3;
    4.  
    5. function Update ()
    6. {
    7.     transform.position = Vector3.Lerp (pointA.position, pointB.position, Time.time * speedNPC);
    8. }
    And now the tricky part: making him going back. I tried several "if" conditions, but without success, like this:
    Code (csharp):
    1. var pointA : Transform;
    2. var pointB : Transform;
    3. var speedNPC : float = 0.3;
    4.  
    5. function Update ()
    6. {
    7.     transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
    8.     if (transform.position.z == pointA.position)
    9.         this.transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
    10.     if (transform.position.z == pointB.position)
    11.         this.transform.position = Vector3.Lerp(pointB.position, pointA.position, Time.time * speedNPC);
    12. }
    Quite hard for a non-programmer...
     
  4. Joppel

    Joppel

    Joined:
    Aug 4, 2012
    Posts:
    34
    I'm no expert so my solution might not be the fanciest.
    You should probably not use function Update() instead use another trigger, Start() perhaps. Using Update triggers the script all the time, you only want to trigger it once and then loop it until you want the NPC to do something else.
    You can check how long it takes for him to move from a to b and add:

    Code (csharp):
    1. var walkTime : float = 5.0
    2. transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
    3. yield WaitForSeconds(walkTime);
    4. transform.position = Vector3.Lerp(pointB.position, pointA.position, Time.time * speedNPC);
    5. yield WaitForSeconds(walkTime);
    either put in a loop or trigger the script again after the second yield.
     
  5. LibduP

    LibduP

    Joined:
    Aug 30, 2012
    Posts:
    8
    Thank you for your help. I tried your script but it only placed my NPC at the point A. Doesn't make it move.
     
  6. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    Can this help?
    Code (csharp):
    1.  
    2. var pointA : Transform;
    3. var pointB : Transform;
    4. var speedNPC : float = 5;
    5. var minDistance : float = 5;
    6.  
    7. function Update ()
    8. {
    9.     // Moving forward
    10.     this.transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
    11.  
    12.     // check if position reach
    13.     var dist = Vecto3.Distance(this.tranform.position, pointB.position);
    14.  
    15.     if(dist<minDistance) {
    16.         // Change position's
    17.         var temp : Transform;
    18.         temp = pointA;
    19.         pointA = pointB;
    20.         pointB = temp;
    21.     }
    22. }
    23.  
    Havent testet the code..!
     
    Last edited: Aug 30, 2012
  7. LibduP

    LibduP

    Joined:
    Aug 30, 2012
    Posts:
    8
    Thank you but doesn't work either... my NPC still move correctly to point B, but stay at this point...

    In your code, when I launch it, I have this warning: "NullReferenceException: Object reference not set to an instance of an object" concerning this line of your code:
    Code (csharp):
    1. var dist = Vector3.Distance(this.tranform.position, pointB.position);
    I'm currenlty trying something using a switch. we'll see.

    Thank you by the way for your help everybody.
     
  8. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    yes seem i forgot transform

    Code (csharp):
    1.  
    2. var dist = Vector3.Distance(this.tranform.position, pointB.transform.position);
    3.  
     
  9. Kinos141

    Kinos141

    Joined:
    Jun 22, 2011
    Posts:
    969
    Use an array of transforms, then manually loop though them once min distance is reached.
    C# example
    Code (csharp):
    1.  
    2.  
    3. Transform[] points;
    4. int currentPoint = 0; // it's like a manual for loop
    5.  
    6. void Update()
    7. {
    8.     var dist = Vector3.Distance(transform.position, points[currentPoint].position)
    9.     if(currentPoint < points.Length) // this is to check if it's less than the length of the points
    10.     {
    11.         if( dist < minDistance)
    12.             Vector3.Lerp(blah blah);
    13.         else
    14.             currentPoint++; // this will manual increment it
    15.        
    16.     }
    17.     else
    18.     {
    19.         currentPoint = 0; //this is to loop it back to zero again
    20.     }
    21. }
    22.  
    23. }
    24.  
    You can then assign the points so that they're in the order you wish them to be in, i.e pointsA = 0, pointsB = 1, etc
     
    Last edited: Aug 31, 2012
  10. LibduP

    LibduP

    Joined:
    Aug 30, 2012
    Posts:
    8
    Hi guys

    Using snortop's code, I managed to get what I wanted, and even more: my NPC is now making a half-turn each time he reaches point A or B.

    Here is what I got:
    Code (csharp):
    1. var pointA : Transform;
    2. var pointB : Transform;
    3. var destination : Transform;
    4. var speedNPC : float = 1;
    5.  
    6. function Update ()
    7. {
    8.     // Moving forward
    9.     this.transform.position = Vector3.Lerp(this.transform.position, destination.position, Time.deltaTime * speedNPC);
    10.  
    11.     // Check if position is reached
    12.     var dist = Vector3.Distance(this.transform.position, destination.position);
    13.    
    14.     if (dist <= 1)
    15.     {
    16.         // Changes position
    17.         if (destination == pointA)
    18.         {
    19.             destination = pointB;
    20.             transform.eulerAngles = Vector3(0,0,0);
    21.         }  
    22.             else
    23.             {
    24.             destination = pointA;
    25.             transform.eulerAngles = Vector3(0,180,0);
    26.             }
    27.     }
    28. }
    So thank you everyone, see you on the forum.
     
    Last edited: Sep 6, 2012