Search Unity

Gravity effect not being overcome by trigger.

Discussion in 'Getting Started' started by Smitty3264, Oct 24, 2020.

  1. Smitty3264

    Smitty3264

    Joined:
    Sep 12, 2020
    Posts:
    38
    I got this code from a source and it works fine for this pool game. I want the ball to roll in the pocket if its say half over the edge and into a trigger that sends the ball under and towards the center of the table. I was having a problem though as the trigger event was not overcoming the gravity so the ball wont move anywhere except to the center of gravity so I modified the code slightly to this:

    Code (CSharp):
    1.  
    2.     [RequireComponent(typeof(SphereCollider))]
    3.     public class Sucking : MonoBehaviour
    4.     {
    5.  
    6.         [SerializeField] public float GRAVITY_PULL = 5f;
    7.  
    8.         public static float m_GravityRadius = 1f;
    9.  
    10.         void start()
    11.         {
    12.             m_GravityRadius = GetComponent<SphereCollider>().radius;
    13.         }
    14.  
    15.  
    16.         void OnTriggerStay(Collider other)
    17.         {
    18.  
    19.             if (other.attachedRigidbody)
    20.             {
    21.                 float gravityIntensity = Vector3.Distance(transform.position, other.transform.position) / m_GravityRadius;
    22.  
    23.                 other.attachedRigidbody.AddForce((transform.position - other.transform.position) * gravityIntensity * other.attachedRigidbody.mass * GRAVITY_PULL * Time.smoothDeltaTime);
    24.  
    25.                 Debug.DrawRay(other.transform.position, transform.position - other.transform.position);
    26.             }
    27.         }
    28.     }
    29.  
    All I did was change void awake to void start I can't try this code out right now as I'm trying to fix errors in my trigger but maybe someone can tell me if this is enough so it wont update every frame toward the center of gravity once the trigger is entered. if not maybe you can tell me can I put the if statement under a fixed update and add forcemode.Impulse? thanks guys