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

How to make an object move up and down continuously for the entire game in javascript

Discussion in '2D' started by tennisboy08, Feb 17, 2015.

  1. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Basically I'm trying to get the main game character to move up and down on the screen continuously while the player gets to move left and right on his own. I already have the left and right finished, but I'm not sure how to make it move up by itself then be triggered to move down when it hits an invisible trigger. Please help me!
     
  2. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    theANMATOR2b likes this.
  3. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
  4. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    depends how you move it left and right.

    If you're using a rigidbody you could do something like:
    Code (CSharp):
    1. bool movingUp = true;
    2.  
    3. if (movingUp){ rigidbody.velocity = new vector2 (0,moveSpeed);}else { rigidbody.velocity = new vector2(0,-moveSpeed);}
    4.  
    5. void OnTriggerEnter(Collider other){
    6.  
    7. if (other.tag == "top"){movingUp = false;}
    8.  
    9. if (other.tag == "bottom"){movingUp = true;}
    10.  
    11. }
    You'd have to play around it a bit to make it stop/move correct and look right, etc but generally that should work.
     
  5. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    I'm actually using lerp
    I'm actually using lerp
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var smooth : float = 8;
    4. private var wait : float = .25;
    5. private var yaxis : float = 1;
    6.  
    7.  
    8. private var newPosition : Vector3;
    9.  
    10.  
    11.  
    12. private var positionA : Vector3 = new Vector3(-3.5, yaxis, 0);
    13. private var positionB : Vector3 = new Vector3(0, yaxis, 0);
    14. private var positionC : Vector3 = new Vector3(3.5, yaxis, 0);
    15.  
    16.  
    17. function Awake ()
    18. {
    19.  
    20.     newPosition = transform.position;
    21.     newPosition = positionB;
    22. }
    23.  
    24.  
    25. function Update ()
    26. {
    27.     yaxis = transform.position.y;
    28.     PositionChanging();
    29.  
    30. }
    31.  
    32.  
    33. function PositionChanging ()
    34. {
    35.  
    36.  
    37.         if(newPosition == positionB)
    38.         {
    39.             if(Input.GetKeyDown(KeyCode.A))
    40.             newPosition = positionA;
    41.             yield WaitForSeconds(wait);
    42.         }      
    43.         if(newPosition == positionB)
    44.         {
    45.             if(Input.GetKeyDown(KeyCode.D))
    46.             newPosition = positionC;
    47.             yield WaitForSeconds(wait);
    48.         }
    49.         if(newPosition == positionA)
    50.         {
    51.             if(Input.GetKeyDown(KeyCode.D))
    52.             newPosition = positionB;
    53.             yield WaitForSeconds(wait);
    54.         }
    55.         if(newPosition == positionC)
    56.         {
    57.             if(Input.GetKeyDown(KeyCode.A))
    58.             newPosition = positionB;
    59.             yield WaitForSeconds(wait);
    60.         }
    61.  
    62.  
    63.  
    64.     transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    65. }
    66.  
     
  6. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    Lerp is just a way of moving from point A --> Point B over a period of time. You're moving it with transform.position.

    It depends on what axis you're moving on but assuming you want to go up and down on Y, you would use the above but replace the rigidbody piece with something like below, flipping the - moveSpeed for +moveSpeed depending on which direction you want to go (up or down).

    Code (CSharp):
    1. float moveSpeed = 1 * Time.deltatime;
    2.  
    3. transform.position = new Vector3(transform.position.x, transform.position.y - moveSpeed, transform.position.z);
     
  7. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Will this still work with JavaScript because I'm using JavaScript
     
  8. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    try this. Make sure that the top and bottom colliders are set as triggers and are tagged "top" and "bottom".

    Code (CSharp):
    1. #pragma strict
    2. private var smooth : float = 8;
    3. private var wait : float = .25;
    4. private var yaxis : float = 1;
    5. private var movement : float;
    6. private var newPosition : Vector3;
    7. private var myTransform : Transform;
    8. private var movingUp : boolean;
    9. private var positionA : Vector3 = new Vector3(-3.5, yaxis, 0);
    10. private var positionB : Vector3 = new Vector3(0, yaxis, 0);
    11. private var positionC : Vector3 = new Vector3(3.5, yaxis, 0);
    12. function Awake ()
    13. {
    14.      myTransform = transform;
    15.     newPosition = positionB;
    16.     myTransform.position = newPosition;
    17. }
    18. function Update ()
    19. {
    20.     yaxis = myTransform.position.y;
    21.     PositionChanging();
    22.      movement = 1 * Time.deltaTime;
    23.  
    24. }
    25. function FixedUpdate()
    26. {
    27.      //If moving up, move +y
    28.      if(movingUp){
    29.    
    30.      myTransform.position = new Vector3(myTransform.position.x, myTransform.position.y + movement, myTransform.position.z);
    31.    
    32.      //If moving down, move -y
    33.      }else{
    34.    
    35.      myTransform.position = new Vector3(myTransform.position.x, myTransform.position.y - movement, myTransform.position.z);
    36.    
    37.      }
    38.  
    39. }
    40.  
    41. function PositionChanging ()
    42. {
    43.         if(newPosition == positionB)
    44.         {
    45.             if(Input.GetKeyDown(KeyCode.A))
    46.             newPosition = positionA;
    47.             yield WaitForSeconds(wait);
    48.      
    49.             if(Input.GetKeyDown(KeyCode.D))
    50.             newPosition = positionC;
    51.             yield WaitForSeconds(wait);
    52.         }
    53.         if(newPosition == positionA)
    54.         {
    55.             if(Input.GetKeyDown(KeyCode.D))
    56.             newPosition = positionB;
    57.             yield WaitForSeconds(wait);
    58.         }
    59.         if(newPosition == positionC)
    60.         {
    61.             if(Input.GetKeyDown(KeyCode.A))
    62.             newPosition = positionB;
    63.             yield WaitForSeconds(wait);
    64.         }
    65.     myTransform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    66. }
    67.  
    68. //Checks collision
    69. function OnTriggerEnter(other : Collider)
    70. {
    71.  
    72. if (other.tag == "top"){movingUp = false;}
    73. if (other.tag == "bottom"){movingUp = true;}
    74. }
    75.  
    76.  
     
  9. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    I just tried this and it for some reason starts at 0,1,0 and just stays there
     
  10. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    You need to change the movement value to make your setup. It should be moving, it's just moving too slow.