Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get the Object itself that entered OnTriggerEnter

Discussion in 'Physics' started by LifeSymbiont, Oct 19, 2021.

  1. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    I am currently trying to make a Planetary gravitation script.
    Because I want to make things nice and performant, I want to access the object itself that entered the OnTriggerEnter areal to make it movetowards the planet.

    This is currently my Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlanetaryGravitation : MonoBehaviour
    6. {
    7.     public float gravityvalue = 20f;
    8.     public GameObject Planet;
    9.  
    10.     void OnTriggerEnter(Collider col)
    11.     {
    12.         if (col.gameObject.tag == "Gravitationable")
    13.         {
    14.             col.gameObject.transform.position = Vector3.MoveTowards(col.gameObject.transform.position, Planet.transform.position, gravityvalue * Time.deltaTime);
    15.         }
    16.     }
    17. }
    18.  
    In engine I don't get any Bugs, but nothing really happens. Does anyone know the solution?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,315
    That's "Collider col".

    You never said this callback never happens, just "nothing really happens" which could mean anything. In the very least, if you're new, don't add stuff in and include conditional checks then some work because you don't know if the callback is happening or if the tag is indeed "Gravitationable" (Is that even spelt correctly?) etc. Verify if the callback is happening with a Debug.Log in the very least or better still, attached the debugger and put in a breakpoint. If you've done this, then please mention it.
     
  3. LifeSymbiont

    LifeSymbiont

    Joined:
    Apr 19, 2015
    Posts:
    36
    Hello, thank you for the response, I managed to get the script working.