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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I don't know why it wont let me move up smoothly - Help

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

  1. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    So when I run my program it should automatically move up while letting the user change which lane he/she will be in lane A B or C. I've tested each feature (moving up automatically, and letting the player switch lanes) separately, but for some reason when I go to put them together its very glitchy! It should smoothly move up while also being able to move left and right, but for some reason the moving up makes it slightly move up and down rapidly while also moving up, and when you change lanes it shoots up and then back down (when I combined them) Please Help me!!

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

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Maybe try to use GetKeyUp instead of GetKeyDown, because the position will continuously be reset if you hold the key down. Also remove FixedUpdate() from Awake() function, not sure what its doing there.
     
  3. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Thank you that actually helped a lot because the moving left to right was a little weird but now its very smooth! But it still seems to rapidly move up and down while moving slowly and glitchily upward.
     
  4. justJack

    justJack

    Joined:
    Feb 5, 2015
    Posts:
    22
    Dude...
    The problem is: you are messing with your position as you are changing it through FixedUpdate and Update. You should not.

    By default, the FixedUpdate is called 50x per second, and the Update method is called as much as your machine is capable of processing.

    Now can you imagine where the mistake is? Maybe the Update method is being called more than the FixedUpdate, or maybe not. Either case, you are changing your position asynchronously.

    You have to decide if you want to move your object by changing your transform or by applying force.
    If you choose the first option, remember to check "isKinematic" on the rigidbody component, then remove the FixedUpdate and will you be fine.
    If you choose the second option, forget the Update and do everything you need to on FixedUpdate.
     
  5. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Indeed. I didn't even see the fixedupdate function in the code...
     
  6. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Thank you very much your comment helps a lot, but how do i just change the y position?
     
  7. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    I'm sorry i'm a noob with unity but I tried this with no luck. Am I on the right track?
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var speed : float = 0.015f;
    4. private var smooth : float = 8;
    5. private var wait : float = 0.15;
    6. private var ycoord : float;
    7.  
    8. private var newPosition : Vector3;
    9.  
    10. private var positionA : Vector3 = new Vector3(-3.5,ycoord, 0);
    11. private var positionB : Vector3 = new Vector3(0,ycoord, 0);
    12. private var positionC : Vector3 = new Vector3(3.5,ycoord, 0);
    13.  
    14.  
    15. function Awake ()
    16. {
    17.     newPosition = transform.position;
    18.     newPosition = positionB;
    19. }
    20.  
    21.  
    22. function Update ()
    23. {
    24.  
    25.     PositionChanging();
    26.     ycoord = transform.position.y += speed;
    27. }
    28.  
    29.  
    30. function PositionChanging ()
    31. {
    32.  
    33.  
    34.         if(newPosition == positionB)
    35.         {
    36.             if(Input.GetKeyUp(KeyCode.A))
    37.             newPosition = positionA;
    38.             yield WaitForSeconds(wait);
    39.         }      
    40.         if(newPosition == positionB)
    41.         {
    42.             if(Input.GetKeyUp(KeyCode.D))
    43.             newPosition = positionC;
    44.             yield WaitForSeconds(wait);
    45.         }
    46.         if(newPosition == positionA)
    47.         {
    48.             if(Input.GetKeyUp(KeyCode.D))
    49.             newPosition = positionB;
    50.             yield WaitForSeconds(wait);
    51.         }
    52.         if(newPosition == positionC)
    53.         {
    54.             if(Input.GetKeyUp(KeyCode.A))
    55.             newPosition = positionB;
    56.             yield WaitForSeconds(wait);
    57.         }
    58.  
    59.  
    60.  
    61.     transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    62. }
     
  8. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4.     ycoord = transform.position.y += speed;
    5.     newPos = Vector3(transform.position.x, ycoord, transform.position.z);
    6.     transform.position = newPos;
    7. }
     
  9. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Thank you! But now it moves up and down but not left and right. Any suggestions?

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

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    the transform.position in PositionChanging is overwritten by the transform.position in Update.

    Change the x position in Update from the transform to the based off the PositionChanging function and make a new Vector3 to store it. Something like:

    newPosition = Vector3(changePosition.x, ycoord,transform.position.z);

    and in the PositionChanging function replace "newPosition" with "changePosition".

    This will ensure that you're changePosition is always the intended a, b, or c and update is always referencing the correct x variable of that position.
     
  11. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    Ok haha I think we're almost there, but its giving me an error saying that it doesn't take a vector3 as a parameter. Did I misunderstand your directions? Heres my code
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var speed : float = 0.015f;
    4. private var smooth : float = 8;
    5. private var wait : float = 0.15;
    6. private var ycoord : float;
    7.  
    8. private var newPosition : Vector3;
    9.  
    10. private var positionA : Vector3 = new Vector3(-3.5,ycoord, 0);
    11. private var positionB : Vector3 = new Vector3(0,ycoord, 0);
    12. private var positionC : Vector3 = new Vector3(3.5,ycoord, 0);
    13. private var changePosition : Vector3;
    14.  
    15. function Awake ()
    16. {
    17.     newPosition = transform.position;
    18.     newPosition = positionB;
    19. }
    20.  
    21.  
    22. function Update ()
    23. {
    24.  
    25.     PositionChanging();
    26.     ycoord = transform.position.y += speed;
    27.     var newPos : Vector3 = new Vector3(changePosition, ycoord, transform.position.z);
    28.     transform.position = newPos;
    29. }
    30.  
    31.  
    32. function PositionChanging ()
    33. {
    34.  
    35.  
    36.         if(changePosition == positionB)
    37.         {
    38.             if(Input.GetKeyUp(KeyCode.A))
    39.             changePosition = positionA;
    40.             yield WaitForSeconds(wait);
    41.         }      
    42.         if(changePosition == positionB)
    43.         {
    44.             if(Input.GetKeyUp(KeyCode.D))
    45.             changePosition = positionC;
    46.             yield WaitForSeconds(wait);
    47.         }
    48.         if(newPosition == positionA)
    49.         {
    50.             if(Input.GetKeyUp(KeyCode.D))
    51.             newPosition = positionB;
    52.             yield WaitForSeconds(wait);
    53.         }
    54.         if(changePosition == positionC)
    55.         {
    56.             if(Input.GetKeyUp(KeyCode.A))
    57.             changePosition = positionB;
    58.             yield WaitForSeconds(wait);
    59.         }
    60.  
    61.  
    62.  
    63.     transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    64. }
    65.  
     
  12. Senladar

    Senladar

    Joined:
    Jan 1, 2015
    Posts:
    45
    you want changePosition.x in your "var newPos":

    var newPos : Vector3 =new Vector3(changePosition.x, ycoord, transform.position.z);

    You only want the X part of it, not the entire Vector3 :)
     
  13. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
  14. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    this is what it looks like.
    it starts back at the igloos but when i hit play it just stays at the middle

    heres the code
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var speed : float = 0.015f;
    4. private var smooth : float = 8;
    5. private var wait : float = 0.15;
    6. private var ycoord : float;
    7.  
    8. private var newPosition : Vector3;
    9.  
    10. private var positionA : Vector3 = new Vector3(-3.5,ycoord, 0);
    11. private var positionB : Vector3 = new Vector3(0,ycoord, 0);
    12. private var positionC : Vector3 = new Vector3(3.5,ycoord, 0);
    13. private var changePosition : Vector3;
    14.  
    15. function Awake ()
    16. {
    17.     newPosition = transform.position;
    18.     newPosition = positionB;
    19. }
    20.  
    21.  
    22. function Update ()
    23. {
    24.  
    25.     PositionChanging();
    26.     ycoord = transform.position.y += speed;
    27.     var newPos : Vector3 = new Vector3(changePosition.x, ycoord, transform.position.z);
    28.     transform.position = newPos;
    29. }
    30.  
    31.  
    32. function PositionChanging ()
    33. {
    34.  
    35.  
    36.         if(changePosition == positionB)
    37.         {
    38.             if(Input.GetKeyUp(KeyCode.A))
    39.             changePosition = positionA;
    40.             yield WaitForSeconds(wait);
    41.         }      
    42.         if(changePosition == positionB)
    43.         {
    44.             if(Input.GetKeyUp(KeyCode.D))
    45.             changePosition = positionC;
    46.             yield WaitForSeconds(wait);
    47.         }
    48.         if(newPosition == positionA)
    49.         {
    50.             if(Input.GetKeyUp(KeyCode.D))
    51.             newPosition = positionB;
    52.             yield WaitForSeconds(wait);
    53.         }
    54.         if(changePosition == positionC)
    55.         {
    56.             if(Input.GetKeyUp(KeyCode.A))
    57.             changePosition = positionB;
    58.             yield WaitForSeconds(wait);
    59.         }
    60.  
    61.  
    62.  
    63.     transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
    64. }
    65.  
     
  15. tennisboy08

    tennisboy08

    Joined:
    Feb 16, 2015
    Posts:
    24
    I don't know why its being so difficult o_O lol