Search Unity

Elevator Move

Discussion in 'Scripting' started by GizmoBradwell, May 12, 2012.

  1. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    I have two elevator scripts, one moves the player smoothly, the other is jerky how do i make it move like the first script does?

    Code (csharp):
    1.  
    2.  
    3. var topMarker : GameObject;
    4. var bottomMarker : GameObject;
    5.  
    6.  
    7. var speed : float = 0.1;
    8.  
    9.  
    10. function FixedUpdate () {
    11.  var weight = Mathf.Cos(Time.time * speed *
    12. 2 * Mathf.PI) * 0.5 + 0.5;
    13.  transform.position =
    14. topMarker.transform.position * weight
    15.       +
    16. bottomMarker.transform.position * (1-weight);
    17. }
    18.  
    19.  
    But this second one below which is triggered not contunually moving, moves the player jerky:


    Code (csharp):
    1.  
    2.  
    3.  
    4.  
    5.  
    6. var topMarker: Transform;
    7.  
    8.  
    9. var bottomMarker: Transform;
    10.  
    11.  
    12. var travelDuration: float;
    13.  
    14.  
    15.  
    16.  
    17.  
    18. var moving: boolean;
    19.  
    20.  
    21. var triggerDisabled: boolean;
    22.  
    23.  
    24. var station: int;  // Station value tell the script where you've placed the
    25. platform start. 0 means bottom, 1 means top.
    26.  
    27.  
    28.  
    29.  
    30.  
    31. function Move() {
    32.  
    33.  
    34.  print("Lift Started");
    35.  
    36.  
    37.  var startPos: Vector3;
    38.  
    39.  
    40.  var endPos: Vector3;
    41.  
    42.  
    43.  
    44.  
    45.  
    46.  if (station == 0) {
    47.  
    48.  
    49.   startPos = bottomMarker.position;
    50.  
    51.  
    52.   endPos = topMarker.position;
    53.  
    54.  
    55.  } else {
    56.  
    57.  
    58.   startPos = topMarker.position;
    59.  
    60.  
    61.   endPos = bottomMarker.position;
    62.  
    63.  
    64.  }
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  var moveStartTime = Time.time;
    71.  
    72.  
    73.  var interp = 0.0;
    74.  
    75.  
    76.  
    77.  
    78.  
    79.  while (interp < 1.0)  {
    80.  
    81.  
    82.   interp = (Time.time - moveStartTime) / travelDuration;
    83.  
    84.  
    85.   transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0, 1,
    86. interp));
    87.  
    88.  
    89.   yield;
    90.  
    91.  
    92.  }
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  transform.position = endPos;
    99.  
    100.  
    101.  station = 1 - station;
    102.  
    103.  
    104.  moving = false;
    105.  
    106.  
    107.  print("Lift Finished");
    108.  
    109.  
    110. }
    111.  
    112.  
    113.  
    114.  
    115.  
    116. function OnTriggerEnter() {
    117.  
    118.  
    119.  if (!moving  !triggerDisabled) {
    120.  
    121.  
    122.   triggerDisabled = false;
    123.  
    124.  
    125.   Move();
    126.  
    127.  
    128.  }
    129.  
    130.  
    131. }
    132.  
    133.  
    134. function OnTriggerExit() {
    135.  
    136.  
    137.  triggerDisabled = true;
    138.  
    139.  
    140.  
    141.  
    142.  
    143. }
    144.  
     
  2. CameronB999

    CameronB999

    Joined:
    Sep 11, 2010
    Posts:
    20
    Does your camera have a move script attached to it or is it stationary? If so, does the camera move during Update, FixedUpdate, or LateUpdate? I had an issue in my game with some elevator movements and it was because my camera was moving at a different time than the elevator which caused a jerkiness.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code 1 could easily be replace with:

    Code (csharp):
    1.  
    2. var topMarker : GameObject;
    3. var bottomMarker : GameObject;
    4. var speed : float = 0.1;
    5.  
    6. function FixedUpdate () {
    7.  var weight = Mathf.Cos(Time.time * speed * 2 * Mathf.PI) * 0.5 + 0.5;
    8.  transform.position = Vector3.Lerp(topMarker.transform.position, bottomMarker.transform.position, weight);
    9. }
    10.  
    The way you wrote it is simply a lerp.

    Code 2 looks like it should work. The only thing that confuses me is that you are not starting your coroutine correctly and possibly something to do with the StepTowards. Reasonably, that should not stop the whole thing from being smooth.

    Here is code 2 rewritten to make a bit more sense:
    Code (csharp):
    1.  
    2. var topMarker: Transform;
    3. var bottomMarker: Transform;
    4. var travelDuration: float;
    5. var moving: boolean = false;
    6. var triggerDisabled: boolean;
    7. var station: int = 0;
    8.  
    9. function Start(){
    10.     transform.position = topMarker.position;
    11.     if(station == 0) transform.position = bottomMarker.position;
    12. }
    13.  
    14. function Move() {
    15.     if(moving) return;
    16.     moving = true;
    17.  
    18.     print("Lift Started");
    19.     var startPos: Vector3 = station == 0 ? bottomMarker.position : topMarker.position;
    20.     var endPos: Vector3 = station == 0 ? topMarker.position : bottomMarker.position;;
    21.    
    22.     var moveStartTime = Time.time;
    23.     var moveEndTime = Time.time + travelDuration;
    24.    
    25.     while (Time.time < moveEndTime)  {
    26.         var interp = (Time.time - moveStartTime) / travelDuration;
    27.         transform.position = Vector3.Lerp(startPos, endPos, interp);
    28.         yield;
    29.     }
    30.    
    31.     transform.position = endPos;
    32.     station = 1 - station;
    33.     moving = false;
    34.     print("Lift Finished");
    35. }
    36.  
    37.  
    38. function OnTriggerEnter() {
    39.     if (!moving) {
    40.         StartCoroutine(Move());
    41.     }
    42. }
    43.  
    44.  
    Oh, also make sure that your camera and player are parented to the elevator when you move it. and un-parent them when it reaches the top.