Search Unity

I have an issue with only one of my triggers being interacted with my player.

Discussion in 'Editor & General Support' started by unity_df-lFUjmS5NU9w, Apr 13, 2021.

  1. unity_df-lFUjmS5NU9w

    unity_df-lFUjmS5NU9w

    Joined:
    Feb 5, 2021
    Posts:
    5
    I have an issue with only one of my triggers being interacted with my player and the others he just passes through. I am somewhat new to understanding code and I am wondering if my code is wrong or why the player won't trigger the other ones?

    Here is my script which I use to make the player camera to slide and appear as if he is actually drifting:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class CameraControlScript : MonoBehaviour
    {
    public float[] Rotz;
    int triggerCount = 0;
    Camera Cam;
    bool CanRotate;
    public float rotSpeed;
    // Start is called before the first frame update
    void Start()
    {
    Cam = Camera.main;
    }
    // Update is called once per frame
    void FixedUpdate()
    {
    if (CanRotate)
    {
    Quaternion canRot = Cam.transform.rotation;
    Quaternion desRot = Quaternion.Euler(0, 0, Rotz[triggerCount - 1]);
    Cam.transform.rotation = Quaternion.Lerp(canRot, desRot, Time.deltaTime * rotSpeed);
    if (canRot.z == desRot.z)
    {
    CanRotate = false;
    }
    }
    }
    private void OnTriggerEnter2D(Collider2D hit)
    {
    Debug.Log("Hit");
    if (hit.CompareTag("Slide"))
    {
    triggerCount = 0;
    triggerCount += 1;
    CanRotate = true;
    }
    }
    }

    Here are some screen shots of the triggers : upload_2021-4-13_12-48-8.png upload_2021-4-13_12-47-45.png

    And this is what the player does which he is supposed to with the first trigger: upload_2021-4-13_12-48-42.png

    And with the others he just doesn't do anything : upload_2021-4-13_12-49-22.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Temporarily destroy the one that is working.

    Does the second one suddenly start working?

    If not, then there's something wrong (layer? position?) of the one that's not working. Start there.

    Also, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    unity_df-lFUjmS5NU9w likes this.
  3. unity_df-lFUjmS5NU9w

    unity_df-lFUjmS5NU9w

    Joined:
    Feb 5, 2021
    Posts:
    5
    Thank you for the response Kurt! When I temporarily deleted the first one the second one suddenly did start working.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    That's really weird.

    So originally, before you deleted the first one in your test, was the "Hit!" you print out showing??
     
  5. unity_df-lFUjmS5NU9w

    unity_df-lFUjmS5NU9w

    Joined:
    Feb 5, 2021
    Posts:
    5
    No the "Hit!" was not showing before I deleted the first one and it didn't show up after I deleted it either
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Okay, fix that first. :)

    Until you get that working nothing else matters.

    Review what needs to happen for OnTriggerEnter2D() to be called. It's all in the docs.

    Also, +1 for using Debug.Log() as it's the best way for interactive games that you don't want to freeze when you hit a breakpoint, but always make sure you are seeing what you expect. That failure to show "Hit!" will turn out to be the root problem here, I am almost certain.
     
  7. unity_df-lFUjmS5NU9w

    unity_df-lFUjmS5NU9w

    Joined:
    Feb 5, 2021
    Posts:
    5
    Thank you so much for your help I seriously appreciate it I found out all I needed was to delete triggerCount = 0; in the OnTriggerEnter2D() and now everything works according to plan!
     
    Kurt-Dekker likes this.