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;
}
}
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.
I hope there’s enough information for anyone to provide help.
Anything would be appreciated.
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
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.
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.
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.
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!
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;
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”
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!