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

I have no idea why this script is acting so weird!!! Help please!!

Discussion in 'Scripting' started by mastamyke, Mar 30, 2014.

  1. mastamyke

    mastamyke

    Joined:
    Feb 5, 2013
    Posts:
    55
    So i have written this script so the ( Tank ) will follow the ( Character ) witch ever way he moves ( right or left ) ... and it only works when im in unity and i start on that exact level the tank is in. But if i start from the level before it and win that level and move to the level with the tank on it , it wont work at all!!! This is really bugging me and would appreciate any help! Thank you!! P.S. This is written in JavaScript

    Code (csharp):
    1. // Transforms to act as start and end markers for the journey.
    2.     var startMarker: Transform;
    3.     var endMarker: Transform;
    4.     var yPosition: float = 0;
    5.    
    6.     // Movement speed in units/sec.
    7.     var speed = 1.0;
    8.    
    9.     // Time when the movement started.
    10.     private var startTime: float;
    11.    
    12.     // Total distance between the markers.
    13.     private var journeyLength: float;
    14.    
    15.     var target : Transform;
    16.     var smooth = 5.0;
    17.    
    18.     function Start() {
    19.         // Keep a note of the time the movement started.
    20.         startTime = Time.time;
    21.        
    22.         // Calculate the journey length.
    23.         journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
    24.     }
    25.    
    26.     // Follows the target position like with a spring
    27.     function Update () {
    28.    
    29.         // Distance moved = time * speed.
    30.         var distCovered = (Time.deltaTime - startTime) * speed;
    31.        
    32.         // Fraction of journey completed = current distance divided by total distance.
    33.         var fracJourney = distCovered / journeyLength;
    34.        
    35.         // Set our position as a fraction of the distance between the markers.
    36.         transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
    37.        
    38.         transform.position.y = yPosition;
    39.     }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Could you explain line 30?
     
  3. mastamyke

    mastamyke

    Joined:
    Feb 5, 2013
    Posts:
    55
    What is happening is that this tank is slowly moving towards the character and tracking the character ( right to left ) to prevent him from passing.. and line 30 is just finding out the time and speed of distance covered of the character ... everything is working perfectly if i play it from that level with the tank, but if im on a previous level and i get to the stage with the "tank" it wont work??? So Weird =\
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Distance covered would use Time.time, not Time.deltaTime
    Code (csharp):
    1.  
    2. // Distance moved = time * speed.
    3. var distCovered = (Time.time - startTime) * speed;
    4.  
     
  5. mastamyke

    mastamyke

    Joined:
    Feb 5, 2013
    Posts:
    55
    already tried that and makes no difference at all =\