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

Constant velocity (ball)

Discussion in 'Scripting' started by Ali_Bakkal, Nov 10, 2013.

  1. Ali_Bakkal

    Ali_Bakkal

    Joined:
    Jan 26, 2013
    Posts:
    84
    Hey guys !

    I have a small question for you. We're working on a game in which the ball must behave in a realistic way : that means it should rebound correctly when hitting a wall or any other object.
    The problem we have is that we want the ball to roll at a constant speed. We tried setting directly its velocity but we don't like the result because the ball doesn't bounce correctly. We also tried to use constantForce.force in a FixedUpdate but the ball was accelerating.

    Is there any way to keep the physics working correctly and force the ball to keep its speed ?

    Thanks in advance, and sorry for my bad english
     
  2. Gaski

    Gaski

    Joined:
    Jul 25, 2011
    Posts:
    84
    When you override the velocity vector don't change its direction. Simply scale its length to the appropriate speed. An quick and easy way to try this is to simply normalize the velocity vector and then scale the resulting unit length vector by your chosen speed and set that as the new balls velocity. This way, the physics system will still calculate the correct direction component of your velocity but you can just control its length / speed/.
     
    Last edited: Nov 10, 2013
  3. Ali_Bakkal

    Ali_Bakkal

    Joined:
    Jan 26, 2013
    Posts:
    84
    Thanks ! It seems to be working. Now is there something like an AddForce that can be used with velocity ?
    Because the AddForce doesn't work. I'm guessing the velocity overrides the velocity changes made by the AddForce.
     
  4. Gaski

    Gaski

    Joined:
    Jul 25, 2011
    Posts:
    84
    Well it was my understanding that you wanted the ball to roll at a constant speed in which case you are just using AddForce to get the ball rolling initially in the direction you want it to roll. If what you mean is you want the ball to be able to never exceed a certain speed then you can choose only clamp the ball velocity if it exceed the desired maximum velocity. If you really do want the ball to be a constant speed then by its very definition you are overriding what AddForce is intending to do...other than setting the balls direction.

    Sorry if I have misunderstood your problem and if so can you elaborate or perhaps show your ball update code as it is atm.
     
    Last edited: Nov 10, 2013
  5. Ali_Bakkal

    Ali_Bakkal

    Joined:
    Jan 26, 2013
    Posts:
    84
    I'm sorry, you actually understood me right, but my answer wasn't clear enough. Now that the ball is rolling at a constant speed, is there a way to make it accelerate the way it would normally do if there was a big impact ?
    I want the player to feel that the ball has been punched or something.
     
  6. Ali_Bakkal

    Ali_Bakkal

    Joined:
    Jan 26, 2013
    Posts:
    84
    pls help !!!
     
  7. iGAMONIC

    iGAMONIC

    Joined:
    Oct 27, 2017
    Posts:
    16
    // it will follow realistic gravity and physics behaviour
    Code (CSharp):
    1. if (playerRB.velocity.magnitude < desiredSpeed)
    2.    {
    3.         playerRB.AddForce(Vector3.forward * moveSpeed);
    4.    }

    // it will not consider gravity and physics behaviour

    Code (CSharp):
    1. playerRB.velocity = Vector3.forward * moveSpeed;
    // its a mixed of constant speed and physics behaviour

    Code (CSharp):
    1. isGrounded = Physics.CheckSphere(landTrigger.position, GroundDistance, 1 << LayerMask.NameToLayer("Ground"), QueryTriggerInteraction.Ignore);
    2.  
    3.    if (isGrounded)
    4.        {
    5.            playerRB.velocity = Vector3.forward * moveSpeed;
    6.         }
    BUT I am still facing a trouble to make my desired ball physics behaviour. i hope my experience will help you enough! :)