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

Question VR Mini Golf - Club and Ball Collision Problem

Discussion in 'VR' started by MrSuter, Mar 21, 2022.

  1. MrSuter

    MrSuter

    Joined:
    Sep 17, 2021
    Posts:
    2
    I can't get the collision between club and ball to be realistic. I've tried using the built in physics with physics materials, both objects' rigidbodies on Continuous Dynamic but it still glitches through and goes in unpredictable directions.

    I'm currently trying a script that just adds force to the ball but similar problematic results.
    Script applied to Club:

    Code (CSharp):
    1. public float ForceAmount = 5.0f;
    2.  
    3.     public void OnCollisionEnter(Collision other)
    4.     {
    5.         if (other.gameObject.tag == "Ball")
    6.         {
    7.             Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
    8.             rb.AddForce(0, 0, ForceAmount, ForceMode.Force);
    9.  
    10.         }
    11.     }
    This project on Github

    Here's a video of what it's doing:
    https://screencast-o-matic.com/watch/c3erltVFKhp

    I want the ball to react to the direction of the club (thereby making a slightly turned club not hit straight, just as in real mini golf) and how hard I'm hitting it (without it ever glitching through of course as it does now). Thanks!
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,455
    Tried decreasing the physics step? So physics will run more per second?
    And maybe just keep code out of this and make sure the club doesn't hit the ground with the collision matrix?
     
  3. justifun

    justifun

    Joined:
    Apr 2, 2013
    Posts:
    22
    I had a similar issue and there was 2 things that resolved it for me. 1. The club needs to be setup to follow your VR controller via physics movements (Physics Move Translation and Physics Move Rotation) instead of simply translating the club or parenting it under the controller. This will keep the rigidbody collisions working properly when it hits the ball. Also make sure there is only one rigid body on the root of the club, the head of it doesn't need one since it will inherit the main one if its a child object.
    You will probably need to set the club to "Interpolate" and "Continuous Dynamic" in the rigid body settings.

    Also set your ground and club to different layers, and then turn off the physics collision matrix in the player settings for them so that they club can go through the ground and not get all wacky.