0

I am a beginner in Unity and I am currently making a simple game. I have a problem where upon initial collision, the event that I wanted to happen does not trigger or in other words the OnCollisionEnter is not working upon initial collision.

The process of my script is when colliding which is the OnCollisionEnter the button will appear or it will be set active to true. But in the first collision it does not trigger or it is not working, I need to walk away a bit and go back in that way it only works. Sometimes I need to do it 2 or more times for the collider to trigger. I don't know if the script has problem or the collider itself but I made sure it is colliding properly when I look at the editor.

I set the object to static so it will not be pushed when walking and colliding towards it. I wonder if that has to do with this problem because even disabled it is the same. But can anyone give suggestions on how not to make the objects move or be pushed away upon collision?

Here is the script for my collision:

using UnityEngine;
using UnityEngine.UI;

public class InteractNPC : MonoBehaviour
{
    //public Button UI;
    [SerializeField] GameObject uiUse, nameBtn;
    private Transform head;
    private Vector3 offset = new Vector3(0, 1.0f, 0);
    // Start is called before the first frame update
    void Start()
    {
        //uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
        uiUse.gameObject.SetActive(true);
        head = transform.GetChild(0);
        uiUse.transform.position = Camera.main.WorldToScreenPoint(head.position + offset);
    }

    // Update is called once per frame
    void Update()
    {
        uiUse.transform.position = Camera.main.WorldToScreenPoint(head.position + offset);
        nameBtn.transform.position = Camera.main.WorldToScreenPoint(head.position + offset);
    }

    private void OnCollisionEnter(Collision collisionInfo)
    {
        //Debug.Log("wews2");
        if(collisionInfo.collider.name == "Player")
        {
            nameBtn.gameObject.SetActive(true);
        }
    }

    private void OnCollisionExit(Collision collisionInfo)
    {
        if(collisionInfo.collider.name == "Player")
        {
            nameBtn.gameObject.SetActive(false);
        }
    }
}

Here is another script with collision and has the same problem:

using UnityEngine;
using UnityEngine.UI;

public class InteractButtonPosition : MonoBehaviour
{

    //public Button UI;
    [SerializeField] GameObject uiUse;
    private Vector3 offset = new Vector3(0, 0.5f, 0);
    // Start is called before the first frame update
    void Start()
    {
        //uiUse = Instantiate(UI, FindObjectOfType<Canvas>().transform).GetComponent<Button>();
        //uiUse = GameObject.FindGameObjectWithTag("ViewButton");
    }

    // Update is called once per frame
    void Update()
    {
        uiUse.transform.position = Camera.main.WorldToScreenPoint(this.transform.position + offset);
    }

    private void OnCollisionEnter(Collision collisionInfo)
    {
        if(collisionInfo.collider.name == "Player")
        {
            Debug.Log("wews");
            uiUse.gameObject.SetActive(true);
        }
    }

    private void OnCollisionExit(Collision collisionInfo)
    {
        if(collisionInfo.collider.name == "Player")
        {
            //Destroy(uiUse.gameObject);
            uiUse.gameObject.SetActive(false);
        }
    }
        
}
Richard Johnson
  • 309
  • 4
  • 17
  • 1
    instead of collision you could use a slightly larger trigger and use that for enabling your button while still be able to collide (= push) with your collider – derHugo Sep 19 '22 at 16:21
  • 1
    You have used logs. Do they properly detect the collision? Right now I'm thinking that everything works exactly right, but not quite how you expect. Since you de active the element as soon as you leave contact your player might be doing a micro bounce where he activates the Button for one frame or even just a physics tick resulting in you not seeing anything. – Voidsay Sep 19 '22 at 17:47
  • @Voidsay The logs does not show in the initial collision as well, the whole `OnCollisionEnter` function does not trigger upon initial collision. I do not understand the thing about the micro bounce but what should I do if that is the case? – Richard Johnson Sep 20 '22 at 06:33
  • @Voidsay I understood what you said with the micro bounce, but it still does not trigger upon initial or first collision. I tried displaying just log without the if statement and still does not work upon initial collision. – Richard Johnson Sep 20 '22 at 06:48
  • I already solved it by using OnTriggerEnter. Thanks – Richard Johnson Sep 20 '22 at 07:56

2 Answers2

1

Try changing Colision detection from Descrete to Continuous collission detection

1

I solved this issue by using OntriggerEnter instead of OnCollisionEnter. In the editor, for the NPC I put a Collider on each part of the body and check the IsTrigger in the parent Object so it will still collide with my character even if the IsTrigger is enabled that will make the character go through the object. I just enlarged the scale of the collider with IsTrigger enabled so the collision will be detected immediately.

Richard Johnson
  • 309
  • 4
  • 17