- Home /
Erratic trigger behavior, I can't find the fault.
Hi guys, I'm kinda at my wits end here. This is my problem:
I have 2 walls opposite of each other. The player can jump off these walls. There is a trigger collider attached to the player object. When the player jumps off one of the walls, this wall becomes "unjumpable" and the opposite becomes "jumpable".
I have this script attached to my player:
public bool proccedLeft = false;
public bool proccedRight = false;
public bool procced = false;
void OnTriggerExit2D (Collider2D other)
{
if(procced)
{
if(other.tag == "LeftWall")
{
proccedLeft = true;
procced = false;
Debug.Log ("LeftProc:" + proccedLeft);
}
if(other.tag == "RightWall")
{
proccedRight = true;
procced = false;
Debug.Log ("RightProc:" + proccedRight);
}
}
}
Both walls share this script:
public class WallControl : MonoBehaviour
{
public GameObject oppositeWall;
private Bouncer jumper;
private Renderer myRend;
void Start ()
{
myRend = renderer;
myRend.renderer.material.color = Color.green;
jumper = GameObject.FindGameObjectWithTag("Player").GetComponent<Bouncer>();
}
void FixedUpdate()
{
if(jumper.proccedLeft && tag == "LeftWall")
{
gameObject.layer = 13;
oppositeWall.layer = 9;
oppositeWall.renderer.material.color = Color.green;
myRend.material.color = Color.red;
print("LeftGone");
jumper.proccedLeft = false;
jumper.procced = false;
}
if(jumper.proccedRight && tag == "RightWall")
{
gameObject.layer = 13;
oppositeWall.layer = 9;
oppositeWall.renderer.material.color = Color.green;
myRend.material.color = Color.red;
print ("RightGone");
jumper.proccedRight = false;
jumper.procced = false;
}
}
Everything works, but sometimes a procc won't register. In other words the player jumps off a wall, but the wall stays available. This happens irregularly, I have playtested for 1 hour and have found no patterns. Now I know that in order for jump bool to be true, the trigger must return true when next to a wall. So the trigger works fine since I have no problems with other collisions.
I have tried many different solutions up until now. Tried sticking the wall logic in Update and even LateUpdate. Tried putting the trigger collider on a child object of the player. Tried separating the wall code and adding it to an empty game object. The result is the same. I even played around with the physics timestep.
I can't find a logical lapse in my code, so it seems to me on paper everything should be functional. The problem occurs not only at high speeds, but at low ones too.
Edit: This is my movement control code:
void FixedUpdate ()
{
float ms = Input.GetAxis ("Horizontal");
// If the player is changing direction (ms has a different sign to velocity.x) or hasn't reached maxSpeed yet...
if(ms * rigidbody2D.velocity.x < maxSpeed)
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * ms * moveSpeed);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
if(jump)
{
//If condition from update is met, jump and then reset the condition to false.
rigidbody2D.AddForce (new Vector2 (gtfo,jumpForce) * comboJumps/2);
jump = false;
//Prevent the player from abusing the wall.
if (player.position.x < startPosition.x)
{
if (walled)
gtfo = repelForce * 20;
else gtfo = repelForce;
}
if (player.position.x > startPosition.x)
{
if (walled)
gtfo = repelForce * 20;
else gtfo = -repelForce;
}
}
}
how are you controlling the character are you using its rigidbody2D?
Colliders are a bit poo if you move an object through them using .position or Translate.
Yes meat is right. If you are moving it using its .position or Transform.translate, then the collisions might be off occasionally. If that is the case I would suggest moving it using its Rigidbody2D.
Answer by Kensei · Aug 07, 2014 at 04:45 PM
OMG I solved it...ridiculous problem. The boolean "procced" was assigned a false value in 2 scripts resulting in biym. Removing jumper.procced = false from the WallControl script solved the issue.
Can't believe it -.-.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160006im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Call a Void on Collision 2 Answers
Triggering candle lights on with Colliders 1 Answer
Multiple triggers on one object still a no? 1 Answer
Triggers Interacting with Triggers 0 Answers
I need help with triggers 1 Answer