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

Question plaform moving weird?(resolved)

Discussion in '2D' started by kirigaya552, Feb 14, 2021.

  1. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    hello guys, i need your help, i created a platform which is supposed to go right then after 2secondes go up after 2secondes goes left etcetera to form a square movement
    i write this code but i dont know why the platform do a diamond movement its very weird
    Code (CSharp):
    1. public class RotatingPlatform : MonoBehaviour
    2. {
    3.     public Rigidbody2D rotatingPlatformRb;
    4.     public float speed = 100f;
    5.     public float firstRotate = 0f;
    6.     public float secondRotate = 2f;
    7.     public float thirdRotate = 4f;
    8.     public float fourthRotate = 6f;
    9.     public float lastRotate = 8f;
    10.  
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.     void Update()
    16.     {
    17.         firstRotate += Time.deltaTime;
    18.         if(firstRotate >lastRotate) firstRotate = 0f;
    19.         if(firstRotate < secondRotate && firstRotate > 0f) moveRight();
    20.         if(firstRotate < thirdRotate && firstRotate > secondRotate) moveUp();
    21.         if(firstRotate < fourthRotate && firstRotate > thirdRotate) moveLeft();
    22.         if(firstRotate < lastRotate && firstRotate > fourthRotate) moveDown();
    23.  
    24.     }  
    25.     void moveRight()
    26.     {
    27.         rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    28.     }
    29.     void moveUp()
    30.     {
    31.         rotatingPlatformRb.velocity = new Vector2 (rotatingPlatformRb.velocity.x, speed * Time.fixedDeltaTime);
    32.     }
    33.     void moveLeft()
    34.     {
    35.  
    36.         rotatingPlatformRb.velocity = new Vector2 ((speed * -1) * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    37.     }
    38.     void moveDown()
    39.     {
    40.         rotatingPlatformRb.velocity = new Vector2 (rotatingPlatformRb.velocity.x, (speed * -1) *  Time.fixedDeltaTime);
    41.     }
    42. }
    43.  
    44.  
    sorry for my english btw
     
  2. unity_g6pUasEkRJ-2jg

    unity_g6pUasEkRJ-2jg

    Joined:
    Jul 24, 2020
    Posts:
    16
    I think I found the problem. Try replacing this line
    rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    with this:
    rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, 0);

    and do the same for the other functions. This way the velocity on the axis you are not using is set to 0

    Also, you might want to change your code so no unnecesary conditions are checked, just to make it more tidy and efficient:
    1. firstRotate += Time.deltaTime;
    2. if(firstRotate >lastRotate) firstRotate = 0f;
    3. else if(firstRotate < secondRotate && firstRotate > 0f) moveRight();
    4. else if(firstRotate < thirdRotate && firstRotate > secondRotate) moveUp();
    5. else if(firstRotate < fourthRotate && firstRotate > thirdRotate) moveLeft();
    6. else if(firstRotate < lastRotate && firstRotate > fourthRotate) moveDown();
     
    kirigaya552 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    Small thing but there's absolutely no need to scale the absolute velocity you're setting by Time.fixedDeltaTime. You might as well be scaling it by some arbitrary value because it serves no purpose.

    The physics system integrates velocity over time to give a position change.
     
    kirigaya552 likes this.
  4. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    but if the fps are not stable, is time.fixeddeltatime not used to make the platform not go faster or slower if the fps fluctuate?
    i heard something like that somewhere
     
  5. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    it worked thanks dude !! and did nt know about else if too i see now thanks
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    FixedUpdate callbacks are used to provide a frame independent callback yes but that doesn't change that you don't need it here. As I said, the velocity will already be time-integrated, that's what the physics system does. Thing about it, if you called the same line 100 times it'll result in the same velocity right because you're setting it to a constant value?
     
    kirigaya552 likes this.
  7. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    ohhh i see thank you , i will immediately change it to FixedIUpdate instead of update
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    I didn't say to do that. I said (a few times now) there's no need to use Time.fixedDeltaTime in your calculations as you're just setting a constant value, not changing a value by a certain amount over time. If you were increasing/decreasing (acceleration/deceleration) velocity over time (integrating) then you would use time in your calculations. integrating Acceleration gives velocity change and integrating velocity gives position change. Integrating velocity to give position change is done by the physics system.

    So this...
    Code (CSharp):
    1. rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    ... is the same as this ...
    Code (CSharp):
    1. rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    2. rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    3. rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    4. rotatingPlatformRb.velocity = new Vector2 (speed * Time.fixedDeltaTime, rotatingPlatformRb.velocity.y);
    because it's just setting the velocity to a fixed value and NOT a value over time.

    It only needs to be this:
    Code (CSharp):
    1. rotatingPlatformRb.velocity = new Vector2 (speed, rotatingPlatformRb.velocity.y);
    ... and adjust your speed accordingly because all you've being doing previously is using a scaled-down version of your speed. Your speed will be applied at 1/50th (default of Time.fixedDeltaTime) so just change your speed to be 50 times higher. Time doesn't come into this but you're using it as some constant.

    Hope that makes sense.
     
    Last edited: Feb 15, 2021
    kirigaya552 likes this.
  9. kirigaya552

    kirigaya552

    Joined:
    Jan 4, 2021
    Posts:
    22
    yes yes that s make sense, thank you for all your replies.
     
    MelvMay likes this.