Search Unity

Question Friction between a ball and terrain not working

Discussion in 'Physics' started by Rajivrocks, Oct 16, 2020.

  1. Rajivrocks

    Rajivrocks

    Joined:
    Oct 3, 2019
    Posts:
    15
    Hello there,

    I am trying to make a ball roll on a terrain, when I take my hand of the controls the ball should slowly stop but it doesn't it continues to roll infinitly at that speed which it was moving at. I'm moving the ball by adding force to the rigid body. When I add drag the ball does slow down so physics aren't broken on the ball i think.

    Note: The bounciness does work, but the friction doesn't

    I added a physics material to a Terrain object and put the fricition and dynamic friction to 1 and the ball continues rolling infinitely. Even when I put both values to 100k it doesn't do anything.

    Also, I am using a ball now but friction doesn't work on any of the primitive objects.

    Here is the code I ues to make the ball move.


    private void Start()
    {
    rb = GetComponent<Rigidbody>();
    }
    private void Update()
    {
    Jump();
    }
    private void FixedUpdate()
    {
    Move(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    }
    void Jump()
    {
    if (Input.GetButtonDown("Jump"))
    {
    rb.AddForce(Vector3.up * jumpVar);
    }
    }
    void Move(float xaxis, float yaxis)
    {
    Vector3 direction = new Vector3(xaxis, 0f, yaxis).normalized;
    rb.AddForce(new Vector3(direction.x * speedVar, direction.y, direction.z * speedVar));
    }


    Any help would be greatly appreciated because I'm really stuck atm :(
     
  2. mjh-sakh

    mjh-sakh

    Joined:
    Oct 12, 2020
    Posts:
    10
    Hi Rajivrocks,
    I'm beginner here and here is my understanding. There is the effect of friction on the ball your ball would slide rather roll if no friction as force is applied at the center. At the same time contact point between ball and surface is a point, it's enough to make ball turning but not enough to drag it down. If you changed your ball to cube, for instance, you would observe that it will slowdown.

    So for a ball, you have option to apply drag or angular drag.

    Do following experiments to have better feeling:
    - set friction to 0 and try to push ball - you will see it slides instead (in order to see it, you need to apply some texture on it or observe it in Scene with ball selected);
    - apply big angular drag on ball and keep drag zero, use small but some friction on surface, make force big (your
    speedVar
    ), and you will see that you slide ball again and it does not roll (and it doesn't stop either);
    - apply drag on the ball and small friction (0.1), push it back and forth, and you will see that it will slips: moving back but still rotating forward; increase friction and you will see that you want be able to do it.
     
    garethdenyer likes this.
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    You should be getting your input data from within Update as this is when input is checked for.

    Regardless of the magnitude of your input (even if it is a tiny amount of numerical noise), you generate a normalized vector from it, so the direction vector always has a magnitude of 1. You then apply a force every physics cycle using the scalar speedVar. The ball will never stop as you are constantly applying force to it.

    Have a private force vector for the class, set this to the required force from the update process depending on input. You can test the magnitude of the input in Update and either set the force or zero it. Use this vector to AddForce in FixedUpdate.

    Either add some drag, or apply a drag force in FixedUpdate (I recommend the latter as it is far easier to control).

    Added:
    The ratio of FixedUpdate to Update is not a constant, so if you want precise control over acceleration (from the duration of applied force) you need to code that in.
     
  4. Rajivrocks

    Rajivrocks

    Joined:
    Oct 3, 2019
    Posts:
    15
    Thanks a bunch for all the help guys, I'll try and implement all the suggestions today and see if I get the wanted behaviour!