Search Unity

Constant speed

Discussion in 'Physics' started by pedrohb88, Sep 19, 2017.

  1. pedrohb88

    pedrohb88

    Joined:
    Aug 23, 2017
    Posts:
    16
    I'm sorry for this really dumb question, but... How do i move a ball in a constant speed? I have a ball that uses gravity and a platform, all that i want to do is move the ball in a constant speed in four directions. I've tried to do so...

    Code (CSharp):
    1. void Update () {
    2.        
    3.         if(Input.GetKeyDown("up")){
    4.             rb.constraints = RigidbodyConstraints.None;
    5.             rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
    6.             rb.velocity = new Vector3 (0, 0, speed);
    7.            
    8.  
    9.         }
    10.         if(Input.GetKeyDown("right")){
    11.             rb.constraints = RigidbodyConstraints.None;
    12.             rb.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
    13.             rb.velocity = new Vector3 (speed, 0, 0);
    14.         }
    15.         if(Input.GetKeyDown("left")){
    16.             rb.constraints = RigidbodyConstraints.None;
    17.             rb.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
    18.             rb.velocity = new Vector3 (-speed, 0, 0);
    19.         }
    20.         if(Input.GetKeyDown("down")){
    21.             rb.constraints = RigidbodyConstraints.None;
    22.             rb.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY;
    23.             rb.velocity = new Vector3 (0, 0, -speed);
    24.          
    25.         }
    26.     }
    But the ball gets slower over time.
     
  2. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    How do you mean?
    Does the ball get slower while holding the key? Or does it get slower after letting go of the key?

    Remember you're using the physics engine, so your ball will be subject to friction and drag. Try setting Drag on the ball to 0. And also try creating Physics Materials for the ball and the platform that has Friction set to 0 (make it like ice)
     
  3. pedrohb88

    pedrohb88

    Joined:
    Aug 23, 2017
    Posts:
    16
    Holding the key or releasing it does not matter, it gets slower anyway x.x Like it gets an IMPULSE and quickly lose speed. The DRAG is already 0. I've tried to use the "zero friction" material, it works very well, but the ball does not ROTATE anymore :/ (btw, thanks for the help)

    EDIT1: I check the speed over the time without the zero friction material and... With ANY speed i put, the ball loses speed until reaches 3.5 and remains at 3.5.
     
    Last edited: Sep 20, 2017
  4. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    Sorry, don't know where my brain was when I first replied.

    "GetKeyDown" is only true for the FRAME that it is pressed, and false afterward. If you need to detect a HELD key, use "GetKey"
     
  5. pedrohb88

    pedrohb88

    Joined:
    Aug 23, 2017
    Posts:
    16
    Yes. Do you have any idea why the problem said in the EDIT1 up there is happen?
     
  6. Plystire

    Plystire

    Joined:
    Oct 30, 2016
    Posts:
    142
    Hmmm, not sure. What does it do with the 0 friction physics materials? Have you also tried it without the constraints being set?