Let me explain. You know how on the game called Flappy Bird when the bird touches the object, he dies? Well that's how I want it to be. When my character/player touches any of the objects that are spawning, I want him to die instantly along with a death animation. Since I'm making an infinite runner, he needs to die instantly. I already tried to do it on my own, but failed multiple time even though it's such a simple concept. You'll be seeing me a lot in the near future most likely, so sorry. Anyways here's the script I made it's incomplete so it doesn't look good, hope you guys can help me: http://pastebin.com/kL09W9DD
Would be simple really. First, make sure both the player and the objects have rigidbodies (make sure the objects don't have gravity enabled though, so they don't fall. and restrict there position and rotation). Then, once the collision happens disable the players controller script, and then change their animation to a death one. If they fall down, the rigidbody with gravity should just make him fall down without his controller affecting him. Code (csharp): public class PipeHandler : MonoBehaviour { private bool _isDead = false; void Update() { Debug.Log(_isDead); } OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Player") { _isDead = true; collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false; } } }
Thanks for the script, but it doesn't work sadly. Here's what I did. I fixed my script, then I added rigidbody2D to my individual objects in the prefab folder and disabled/enabled gravity which didn't do anything. It either stayed the same or started to float. I added my script and a rigidbody2D to my gameobject called Instantiate (it spawns the objects in the place I want it to) and my player which still didn't do anything. Then I tried to add it to my individual objects in the prefab folder, which only caused lag. How can something so simple be so hard for me?
There seems to be a lot of things that can break collisions. You don't need both objects to have rigidbodies, but at least one must have a non-kinematic rigidbody. The non-kinematic part seems to be left out of the tutorials and documentation and trips a lot of people up.
My rigidbody2D on my player was already non-kinematic. So that doesn't seem to be the problem. Could it possibly be the script that's the problem? Or maybe scripts attached to my player?
Perhaps a bit obvious though have you selected the 'Player' tag to the player object from the Inspector?
First of all you guys are missing the "2D" in your method names.... http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html Anyway, it should be as simple as this... 1) Create a player with a rigid body and a collider 2) Create an obstacle with a kinematic body and a collider 3) Attach a script to the player, including the method OnCollisionEnter2D(). Upon collision do.... a) Switch the graphic of the player to a dead image b) Disable the controls of the player c) Set the velocity of the player to zero
For choice B, you mean like this?: collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false; Or like this: collision.gameObject.SendMessage("Message"); Oh and let me explain how my obstacles are. There are two obstacles in one prefab called object. I attach the object to a gameobject called Instantiate. I needed to do this in order to have them spawn at different places and to spawn infinitely. So should I attach a kinematic body to my individual obstacles, my object, or my Instantiate?
If you mean if my player collides with an obstacle, then yes. But if you mean does it appear on the console, then no. So no it does not seem to detect collisions at all in the console. Unless of course UnityEngine.Debug:Log(Object) means that it detects it. What should it say or do if my player collides with an obstacle?
As a test try putting this in your script. It should output to the Console when a collision occurs... void OnCollisionEnter2D(Collision2D other) { print("OnCollisionEnter2D: " + other.gameObject.name); }
I did a test, it still didn't work. I tried messing with the Rigidbodies and the colliders again, but nothing seems to work. Maybe I put it in wrong. Is the script supposed to replace: OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.tag == "Player") { isDead = true; collision.gameObject.GetComponent<PlayerControllerScript>().enabled = false; Or is it supposed to go with it like: void OnCollisionEnter2D(Collision2D other) { print("OnCollisionEnter2D: " + other.gameObject.name); _isDead = true;
It didn't work, and I had really high hopes for this one to. Maybe it has something to do with the 2D tools. I heard from some people that the 2D tools aren't working correctly in Unity. Is this true? If so should I try to use the 3D tools to make a 2D game?
It should work. Many people including myself are successfully using the 2D tools even though there might be a bug here or there. I think you should analyze this sample project to see how they have everything setup with proper collision occurring... https://www.assetstore.unity3d.com/#/content/11228
Okay then. I'll analyze this game and see how their colliders and scripts are compared to mine, but it will have to wait. I've been trying to fix this problem since 3:00pm, so I'm tired. Thanks for the link.
Alright I analyzed the scripts and the game objects of the sample you gave me. Everything looks like mine except for a few added stuff like player health, taunts, etc. So now that I looked at that, I have put together a short script for the player death. When you have the time take a look at it to see if it will work or not. So far it doesn't so I know I'm missing something, so please check it out when you have the time. Thanks. http://pastebin.com/jYwhUSiG
Never mind it I finally got it to work. Thank you for all your help. Without you this would've taken longer. Thanks a lot!