- Home /
Infinite Runner Live Training 2d collisions not working
At about 30 minutes into the Infinite Runner Live Training, Mike used a quad to make a 2d Collider to pause the game. I copied the code word for word I'm pretty sure, but the game still doesn't pause. I have been looking over my project for about half an hour now trying to figure out what went wrong, but couldn't find anything. I would appreciate if anyone could tell me what I am doing wrong. Thanks and here's my code.
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2d(Collider2D other)
{
if(other.tag == "Player")
{
Debug.Break ();
return;
}
if(other.gameObject.transform.parent)
{
Destroy (other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
Here's the link to the live training session: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/infinite-runner
Answer by serenefox · Mar 01, 2014 at 09:01 PM
Since it seem nobody else is answering this question I guess I will give it a try.
I looked at the video and I noticed two things that could potentially be a problem.
Your code looks like it would work but since I am not as experienced in it I'm not sure, but the code in the video did not have "return;" in the first if statement.
And 2. Is your player character tagged "Player" in the inspector? And if so does the object to collide have the collider marked as a trigger?
I am pretty sure he included the return to get rid of some $$anonymous$$or error. And yes, I am sure I tagged the character "Player". Have you tried this code yourself?
I have not tried it myself but I am thinking I should, I will get back to you on it. But I do want to ask, are you following to tutorial exactly with all the same assets, or are you implementing this in your own game/project with your own assets? And one more thing you could try in the mean time, put this in the first if statement to see if your character is registering a hit with the collider/trigger.
print("hit");
if nothing shows up in the console when trying the problem it will narrow things down.
So I have tested the script and it worked as it should for me, which means its not a scripting error. I have a few things to ask you:
Does your character have a rigidbody2D on it?(Im sure its on there but I just needed to ask)
and
When I was recreating the problem and I created the "Quad" primitive, I noticed the Quad starts with a $$anonymous$$esh collider, and if its going to work it needs a 2d collider, which the mesh collider is not i don't believe. So I added the "BoxCollider2D" and removed the mesh collider. But other than that, I did not do anything differently.
I don't know what else to try, but let me know if these two questions are true, and if you are still having a problem try the print method and let me know if the console shows anything.
So here's what I have
I am using the sample assets that were used in the video
$$anonymous$$y character does have a rigid body 2d
When I put print ("hit") in the first if statement nothing prints out in the console on run
I am using a box collider 2d on both destroyers, not a mesh collider.
I wanted to ask what your z axis is for the destroyers, main camera, and player and if your character is on a different layer than the rest of the project. Don't know if either of those are causing the problem. In the meantime I'll look at the vid again.
Answer by Cobrryse · Mar 02, 2014 at 02:01 PM
I got it Serenefox! Not sure what I did, I just started from scratch and watched the video again. Thanks for sticking with me throughout it all! Owe you big time!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Player movement script for a stickman 1 Answer
Idle Animation Not Playing 1 Answer
2D Collision Between Two Objects 1 Answer