- Home /
The question is answered, right answer was accepted
OnTriggerEnter2D not triggering
I don't know if it's this is going to be my first 2d prototype or what because I can't see what I'm doing wrong here.
The only thing I thought of that the problem could have been would be that I'm using Transform.Translate to move the player instead of using rigidbody movement functions but I really haven't found too much on rigidbody movement for the way I want the player to move so I don't know what I'm doing wrong, I know I have to have a rigidbody on the player for trigger events to get fired but I have that on my player and my collider on my item is set to trigger and has a tag assigned to it.
Here's a couple images of my project.
Here's my OnTriggerEnter2D code.
private void OnTriggerEnter2D(Collider2D other)
{
print(other.tag);
}
So what do you want to do with trigger what is your aim? I didin`t understand u r question what you need in script.
I just need to get the OnTriggerEnter2D function to fire the rest of the script I know how to do and I know it will work but I have not ever worked with this this function before and the way I've found on other posts and videos does not work at all. I'm just clueless on why this is not working when it most certainly should be.
Answer by AshwinTheGammer · Oct 07, 2017 at 04:29 AM
try this void OnTriggerEnter2D(Collider2D col) { //If collided object is block if (col.GetComponent().tag == "Row") { //My script } }
Yeah my bad on the script, you're right on OnTriggerEnter2D ins$$anonymous$$d of OnCollisionEnter2D but still the function does not fire at all. I will keep trying to figure this out thanks for the reply though.
It means that you have to print the name of gameObject in which the script in. Would u upload the full code tell me the system of ur game. I also want to help u. Please give me a chance.
Yes, I understand how the OnTriggerEnter2D works but the problem is it doesn't fire at all. As for the full code that is all that I have in the player interaction script is the
private void OnTriggerEnter2D(Collider2D other) { print(other.tag); }
The system is I'm trying to have when the player walks over the item I want him to be able to do something(The to-do something I know how to do because I've done that a lot before just never with colliders but with raycasts) but my problem is that the void that should be firing when my player enters that trigger it doesn't fire at all and that's the problem that I'm trying to figure out, I'm starting to think that it's because I move the player with transform.Translate() but I've yet to find movement code for a rigidbody that will work for a top-down movement style that I'm going for($$anonymous$$ovement is sort of like Darkwood/$$anonymous$$iami Heat)
An I appreciate you trying to help me out, don't get me wrong.
Here's my movement script: (Bit messing right now sorry bout that)
if (!isRunning)//WAL$$anonymous$$ING
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
if (transform.rotation.z > 0.70f || transform.rotation.z < -0.70f)
{
transform.Translate(0, -walkSpeed * Time.deltaTime, 0);
}
else
{
transform.Translate(0, walkSpeed * Time.deltaTime, 0);
}
w = true;
}
else
{
w = false;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{
if (transform.rotation.z > 0.70f || transform.rotation.z < -0.70f)
{
transform.Translate(0, walkSpeed * Time.deltaTime, 0);
}
else
{
transform.Translate(0, -walkSpeed * Time.deltaTime, 0);
}
s = true;
}
else
{
s = false;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
if (transform.rotation.z > 0.70f || transform.rotation.z < -0.70f)
{
transform.Translate(walkSpeed * Time.deltaTime, 0, 0);
}
else
{
transform.Translate(-walkSpeed * Time.deltaTime, 0, 0);
}
a = true;
}
Answer by DayzSergeant · Oct 07, 2017 at 06:28 AM
So, after playing around with another component in my game it turns out all I had to to was add a box collider 2d to my player along with the rigidbody 2d. Removed the previous answer that I had posted and this is for anyone who has this problem. Thanks for the help Ashwin, sorry if my question was a bit vague need to work on explaining things a bit better lol
Follow this Question
Related Questions
How can I detect a collision point, but allow player to pass through collider. 1 Answer
Unity2D side collision detection 1 Answer
How do I get the player and the object they touch both disappear/destroyed? 1 Answer
My object is going through boundaries Problem! 1 Answer
Unity getting an object to pass through another object but still trigger collision 1 Answer