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

Ball hitting slope physics

Discussion in 'Physics' started by eliasjgnilsson, Dec 15, 2021.

  1. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Hi,

    I am creating a 3D game where you shoot a ball, which is done by the following code:

    Code (CSharp):
    1. rigidBody.AddForce(shotDirection * power, ForceMode.VelocityChange);
    There is a problem when hitting harder (not that hard though) shots against a slope, the ball will jump straight up or even backwards:

    Namnlös.png
    I would suspect that this has to do with bounciness of surfaces and that it bounces against the slope. I therefore tried to put a physics material on the slope part with a bounciness of 0 and bounce combine minimum. This lead to the ball bouncing less high, but still in the same general direction.

    Is it possible to solve this, to get the ball to travel more "physically accurate", like a normal ball would on a slope? A normal ball would most likely use that little bend when the slope starts to start travelling more and more upwards and not just skip this part and bounce straight up, or even backwards.

    Or is there any other way to solve this problem?
     
  2. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Any ideas?
     
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Have you experimented with changing the rigidbody collision detection on the ball to Continuous Speculative? The fact that you say the problem occurs when you increase the force makes me think this is linked to a timing issue...
     
  4. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Thanks for the suggestion. This makes it work even worse though, now it jumps straight up or backwards even from small powered shots. Dynamic didn't work either.
     
  5. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Can you post a small unity package that replicates the problem, I was not able to break it with a 0.1 scaled sphere striking a 45 degree rotated cube. Edit: unfortunately my time is very limited, so if anyone else wants to have a go, don't wait for me!
     
  6. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    I am sorry, the project is huge and very nested. Not sure I can get it to a nice small package.

    So you can in 3D hit a ball into a slope and it doesn't bounce straight up or even backwards?
     
  7. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Attached, 2020.3.3f1 tested. Excuse the co-routine, this was just to add some delay so my elderly PC had initialised everything before the animation started playing. In case the package does not work:

    New scene, add cube, rotate 45. New sphere, scale 0.1, add rb, - continuous speculative, attach script:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace testzone
    6. {
    7.     public class InitialVel : MonoBehaviour
    8.     {
    9.         [SerializeField] private float velDelta = 10;
    10.         void Start()
    11.         {
    12.             Application.targetFrameRate = 60;
    13.             StartCoroutine(FireBall());
    14.         }
    15.  
    16.         IEnumerator FireBall()
    17.         {
    18.             yield return new WaitForSeconds(1);
    19.             Rigidbody rb = GetComponent<Rigidbody>();
    20.             rb.AddForce(Vector3.right * velDelta, ForceMode.VelocityChange);
    21.         }
    22.     }
    23. }
     

    Attached Files: