Golf ball dynamics

Hi guys

I’m making a Foot Golf type game.

I have a joystick and a button
When you keep the button pressed the power builds up and then the ball gets hit accordingly
direction left and right also works with the joystick. but its far from perfect. The ball if just hit along the ground hardly rolls and i cant seem to get a nice lob effect. ie lower power, higher angle.

lastly, are terrain colliders a bit flakey? as sometimes my ball just disappears and i have friction on the ball too but it never seems to stop rolling, or it hardly rolls when i put friction up.

Was looking on youtube for some type of tutorial but didnt find anything suitable. Wondered if somebody on here that understands these types of dynamics can give me a hand.

If you need me to post videos i can do that too.

here is my code this far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SoccerPlayerController : MonoBehaviour
{
    public AudioSource ballKickSound;
    public float movX;
    public float movY;
    public GameObject ball;
    public Rigidbody ballRB;
    public float ballSpeed;
    public Animator ani;
    private bool kickButtonHeldDown;
    public float kickPower;
    public Text kickPowerIndicator;
    float maxKickPower = 2000;
    float leftRightMagnitude;
    float upDownMagnitude;
    float minKickPower = 500;
    float kickChargeSpeed = 1200f;
    private bool canKickBall = true;


    private void Update()
    {
        CheckKickPower();
        CheckBallMovement();
    }

    public void CheckKickPower()
    {
        if (kickButtonHeldDown && kickPower <= maxKickPower)
        {
            if (kickPower <= minKickPower)
            {
                kickPower += Time.deltaTime * kickChargeSpeed + 720;
            }
            else
            {
                kickPower += Time.deltaTime * kickChargeSpeed;
                if (canKickBall && kickButtonHeldDown)
                {
                    float convertPowerToNumber = kickPower / 140f;
                    //convertPowerToNumber = Mathf.Floor(convertPowerToNumber);
                    convertPowerToNumber -= 4;
                    int canonPower = (int)convertPowerToNumber;
                    kickPowerIndicator.text = canonPower.ToString();
                }
            }
        }
    }

    public void KickButtonDown()
    {

        kickButtonHeldDown = true;
    }

    public void KickButtonReleased()
    {
        kickButtonHeldDown = false;

        KickBall(kickPower);

        kickPower = 0;
    }

    public void KickBall(float shotPower)
    {
        print("in kickball = " + shotPower);
        if (shotPower < 1300)
        {
            ani.SetBool("MediumKick", true);
        }
        else
        {
            ani.SetBool("HardKick", true);
        }

        StartCoroutine(KickDelay(shotPower));

        //ballRB.AddForce(0, shotPower * 2 * Time.deltaTime, 0);
    }

    void CheckBallMovement()
    {
        movX = SimpleInput.GetAxis("Horizontal");
        leftRightMagnitude = movX * 1500;
        movY = SimpleInput.GetAxis("Vertical");
        upDownMagnitude = movY * 1000;
    }

    IEnumerator KickDelay(float shotPower)
    {
        float waiting = 0.8f;
        yield return new WaitForSeconds(waiting);
        ballKickSound.Play();
        ballRB.AddForce(0, 0, shotPower * 2 * Time.deltaTime);
        ballRB.AddForce(0, upDownMagnitude * 2 * Time.deltaTime, 0);
        ballRB.AddForce(leftRightMagnitude * 2 * Time.deltaTime, 0, 0);
        StartCoroutine(ReloadScene());
    }

    IEnumerator ReloadScene()
    {
        float waiting = 6f;
        yield return new WaitForSeconds(waiting);
        SceneManager.LoadScene("Hole1");
    }
}

One thing to for sure look into is the maximal angular velocity. Rigidbodies have a default setting of 7, which is good for performance (apparently?), but it’s way too low for spinning balls, so you’ll probably want to increase it a bunch for your ball. You might also want to give it some spin (by adding torque), in order for it to feel more ball-like.

I don’t think so, but also keep in mind that there are options on the rigidbody to control how accurate the hit detection is. Make sure that the collision mode is set to “continuous” and not “discrete” and you might also have to turn on interpolation for that to work (can’t remember if that’s the case or not). Discrete is the fastest but doesn’t work well enough for important things.

Thanks guys, and what about the actual code for my control of the ball? how can i better it or make it more consistent?

I believe you need to mess around with any friction settings. Drag caused by air etc. And mass of the ball, elasticity.

These parameters will greatly effect how your ball responds to the forces applied.

They can be annoying to balance though. I’ve tried creating a football using real-world data, and it was like a marble!

Cant get a good “Lob” feel, seems to either hit too high or too far, and very errattically.

Not exactly sure what you mean, but maybe you could try playing with the drag and friction. The physics engine tends to behave like you’re in outer space by default, so often you need to add drag to simulate how things slow down in the real world.

Oh, and be sure to scale things properly!

A 10 cm ball falls 1 meter a lot faster than a 1 meter ball falls 10 meters!

Hi guys

What i mean is:

When i hit the ball along the ground at a power of about 5 it rolls x amount without touching up on the joystick.
but with full up control its not going very high, and by increasing up, it seems to increase the distance too. so its very erratic. height & distance by power i mean. If i hit 2 on my power and it rolls 5 meters, but now i had “lob” i want it the same distance but to go up x amount of meters. or whatever level of up was applied by the joystick.

Sounds like real golf - Bravo!

How are you applying force to the ball? Are you actually hitting it with an in-game object?

Cant quite comprehend your problem. But, it sounds like you are applying the same force whilst adding an upward direction. Essentially reducing the lateral force relatively.

Also worth a mention here,

“The Physic Material is used to adjust friction and bouncing effects of colliding objects.”

Hi UKRobo, no im not hitting it with an object, just applying force.
“But, it sounds like you are applying the same force whilst adding an upward direction. Essentially reducing the lateral force relatively.” - yes applying forward force depending on power held in, and upward trajectory depending on how much joystick “Power” is applied. But i do have it working a lot better now. Not perfect, but workable. Also my terrain collider is working really well with my ball now. Not falling through the ground at all. Thanks for the help.