- Home /
Why is this only working for the second object that collides?
I have two game objects that move with user input. Each has a box collider 2d with triggers and a rigid body 2d attached. There is a static game object with tag "Divider" that also has a box collider 2d attached. When a collision of one of the objects occurs with the "Divider," limitX is supposed to then equal false. However, for some reason if object1 collides first the console will log as it is supposed to yet limitX will not switch to false until object2 collides. It doesn't matter which object hits first or second either.
void OnTriggerEnter2D(Collider2D collider) {
Debug.Log ("Triggered: " + collider.name);
if (collider.tag == "Divider") {
limitX = false;
}
}
Did you make absolutely sure the tag is spelled right, capitalized and all? That would seem to be the only problem if you get the Debug.Log.
This is my coding that works for a game, but i have two separate scripting files for both objects that collide. This is the coding where I instantiate the collision. I hope it helps
{
void OnTriggerEnter(Collider otherObject)
{
if (otherObject.tag == "enemy")
{
enemy Enemy = (enemy)otherObject.gameObject.GetComponent("enemy");
}
}
}
Unfortunately I'm still having this issue. I double-checked the spelling and capitalization of the tag in all places and I tried adjusting things based on the code edragonga$$anonymous$$g provided. The thing I find strange is that it does work but only when the second object, whichever that might be, collides with the "Divider." No matter how many times the first object to hit it collides with the "Divider," it is not until the second object hits that it works. I can't figure out why this would be.
Could you package up the script and prefabs and attach it? I can take a look! Its too hard to debugs with so little info =(
I am pretty new to Unity and coding in general, and this is just for a basic pong game that I'm trying to make as practice. With that said, here is all the code. I'm sure it's probably not great but as of now I'm just concerned with this particular issue. Hope it's enough. I appreciate the help!
public class Paddle$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public bool limitX = true;
void Start () {
}
public float paddle$$anonymous$$ovementY = 0.1f;
public float paddle$$anonymous$$ovementX = 0.1f;
void Update () {
GameObject paddleOne = GameObject.FindGameObjectWithTag ("PaddleOne");
GameObject paddleTwo = GameObject.FindGameObjectWithTag ("PaddleTwo");
Vector3 posOne = paddleOne.transform.position;
Vector3 posTwo = paddleTwo.transform.position;
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W)) {
paddleOne.transform.Translate(0, paddle$$anonymous$$ovementY, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.S)) {
paddleOne.transform.Translate(0, -paddle$$anonymous$$ovementY, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) {
paddleOne.transform.Translate(-paddle$$anonymous$$ovementX, 0, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)) {
paddleTwo.transform.Translate(0, paddle$$anonymous$$ovementY, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.DownArrow)) {
paddleTwo.transform.Translate(0, -paddle$$anonymous$$ovementY, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.RightArrow)) {
paddleTwo.transform.Translate(paddle$$anonymous$$ovementX, 0, 0);
}
if (limitX == true) {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftArrow)) {
paddleTwo.transform.Translate(-paddle$$anonymous$$ovementX, 0, 0);
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D)) {
paddleOne.transform.Translate(paddle$$anonymous$$ovementX, 0, 0);
}
}
}
void OnTriggerEnter2D(Collider2D collider) {
Debug.Log ("Triggered: " + collider.name);
if (collider.tag == "Divider") {
limitX = false;
}
}
}
Answer by MousePods · Mar 18, 2014 at 08:30 PM
Ok, So I hope you don't get mad, but I changed and added a few things.
The way you were doing it wasn't going to work because it will turn off both player's movement controls and other issues.
I made sure to comment everything and explain what I did.
Hope this helps!!!
Wow, I really appreciate this. While I did really want to make the whole thing on my own, it seems I am not quite there yet. This will certainly give me some new incite on how things work. Thank you!
Np! Good luck learning Unity, it should be fun =]
Also, could you accept the answer? Thanks!