(Fixed /Closed) If statements not working inside of OnTriggerEnter

Forgive me if this is a really simple problem, I’m new to both Unity and any form of coding.

Expected result:
When the player touches the triggers, depending on the player’s direction of movement, the camera moves +/- 100 on the z axis

Current result:
When the player touches the trigger, it recognises the collision but nothing happens.

Here’s the code I’m using as well as console logs.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerCollision : MonoBehaviour {


    public Transform posPlayer;
    public Transform posCamera;

    public float lastPos;
    public float curPos;

    void OnCollisionEnter (Collision collisionInfo)
    {
        Debug.Log(collisionInfo.collider.name);
    }

    void OnTriggerEnter(Collider collisionInfo)
    {
        Debug.Log(collisionInfo.GetComponent<Collider>().tag);
        if (collisionInfo.GetComponent<Collider>().tag == "CameraBreakpoint")
        {
            if (curPos > lastPos)
            {
                Debug.Log("Camera moved right");
                posCamera.transform.position = new Vector3(posCamera.position.x, posCamera.position.y, posCamera.position.z + 100);
            }

            if (curPos < lastPos)
            {
                Debug.Log("Camera moved left");
                posCamera.transform.position = new Vector3(posCamera.position.x, posCamera.position.y, posCamera.position.z - 100);
            }
        }
    }


    // Use this for initialization
    void Start () {
        curPos = posPlayer.transform.position.z;
    }
 
    // Update is called once per frame
    void FixedUpdate () {
        curPos = posPlayer.transform.position.z;

        // Check which way the player is moving
        if (curPos == lastPos)
        {

        }

        if (curPos > lastPos)
        {
            print("going right");
        }

        if (curPos < lastPos)
        {
            print("going left");
        }

        lastPos = curPos;
    }
}

3001967--223799--upload_2017-3-21_15-28-55.png

The CameraBreakpoint collider is a cube placed underground with a tall verticle box collider as shown here.
The player has a rigidbody and box collider.
The CameraBreakpoint has a box collider with “Is Trigger” ticked.

I was to the right of the pillars and moved to the left of them.
3001967--223802--upload_2017-3-21_15-32-27.png

I hope there’s enough information for anyone to provide help.
Anything would be appreciated. :slight_smile:

Regards,
Saber

ps: Is there a way to change your username?

Note, there is no reason to use GetComponent(), it is already a collider. This could create an issue, but it might not.

I do see your CameraBreakpoint debug printout, but just to make sure. Is that coming from the OnTriggerEnter or the OnCollisionEnter?

Your code on line 24 should work but it is recommended that you use CompareTag() instead since it is much faster and does not generate garbage.

 if (collisionInfo.CompareTag("CameraBreakpoint"))

For this to work as expected the script should not be on the CameraBreakPoint tagged GameObject but perhaps on the player since collisionInfo is from the “other” part hence why it is normally just named other. One of the GameObjects needs to have a Rigidbody component

Thanks for the quick responses, I’ve removed GetComponent() and the OnCollisionEnter parts and the console log looks the same as before.

Changing to CompareTag feels a lot nicer to write, but changing it to search for Player (the player is tagged as Player as well) didn’t change the outcome.

Just to check, did you print out the results of collisionInfo.tag == “CameraBreakpoint” ? and see if it’s returning true?

That is another advantage of CompareTag() it will warn you if you are looking for a tag that does not exist.

1 Like

Brathnann, would that be put at the end of line 17?

Yes, you should be able to add Debug.Log(collisionInfo.tag == “CameraBreakpoint”); After line 17.

Learn how to properly debug and make your life easier.

Attach mono to the unity editor, set a break point in your code, press play, hit the breakpoint, step through the code line by line and see what’s happening and why it’s happening.

1 Like

Adding Debug.Log(collisionInfo.tag == “CameraBreakpoint”); returned true in the log.

I’m going to try Naraku’s response when home ~ 1hour.

Is it possible to play at 1fps to more easily see whats happening?

I would say, that being the case. There is a good chance curpos and lastpos are equal when your OnTriggerEnter happens. Which actually makes sense when I’m looking at it. I believe FixedUpdate will run before the OnTriggerEnter, which means they are the same value when the Trigger happens.

I bet if you do a check for them being equal in your OnTriggerEnter, you’ll get that if check going.

1 Like

On mono, I set a breakpoint on line 17 and it halted the game as expected, putting the breakpoint at line 20 was interesting as it never hit that line, so I’m thinking the if statements are wrong.

I just added
if (curPos == lastPos)
Debug.log(“Here’s the problem”)

and set the breakpoint on the debug line. It halted the game!
3002215--223829--upload_2017-3-21_19-0-9.png

So curPos and lastPos are equal when the OnTriggerEnter is being checked, good call Brathnann, sorry to be a bother, but how would I go about getting the if statements to work now?

Just for reference, here’s the FixedUpdate.

// Update is called once per frame
    void FixedUpdate () {
        curPos = posPlayer.transform.position.z;
        // Check which way the player is moving so the camera knows which was to translate
        if (curPos == lastPos)
        {

        }
        if (curPos > lastPos)
        {
            print("going right");
        }

        if (curPos < lastPos)
        {
            print("going left");
        }


        lastPos = curPos;

3002215--223832--upload_2017-3-21_19-7-10.png

F10 executes the current line and stops on the next so you can see the path it is executing.
You can mouse over the variables to see their values, or query for them in in the Immediate window console on the bottom right: “?VariableName”

1 Like

Pressing f10 then hovering over confirmed both curPos and lastPos are the same value.

Thankyou for the help everyone :slight_smile:

By moving lastPos = currentPos in FixedUpdate to the start rather than the end, it still stores the positions from the frame before but doesn’t interrupt any OnTrigger events!

void FixedUpdate () {
        lastPos = curPos;
        curPos = posPlayer.transform.position.z;
        if (curPos == lastPos)
        {

        }
        if (curPos > lastPos)
        {
            print("going right");
        }

        if (curPos < lastPos)
        {
            print("going left");
        }      
    }

/Thread close