- Home /
Problem with collision detection
Hey so I've got a player moving alone a straight line, and random cubes spawn in front of him randomly. As the player hits a cube it is destroyed and 'hit' is outputted into the console. Once this player has travelled a certain distance (~100), he is repositioned to the beginning of his path.
Again the cubes can spawn in front of him, however there is no collision detection and no cubes are destroyed..
The code for the collision detection (player is 'Player', and cubes are 'lasagnyi'). This script is placed as a spawn script on the lasagnyi gameobject, which refers to a lasagnyi prefab.
GameObject player = GameObject.Find("Player");
int r = Random.Range(0, 500);
if (r < 10)
{
float x = Random.Range(player.transform.position.x, 40.0f);
spawnPosition = new Vector3(x, 7, 0);
GameObject myLasagnyi = (GameObject)Instantiate (lasagnyi, spawnPosition, Quaternion.identity);
}
This code is for the destruction of the lasagnyi and is placed on the playerBehaviour script which is contained in the Player gameobject.
void OnCollisionEnter(Collision collision)
{
Debug.Log("hit");
if(collision.gameObject.name == "lasagnyi(Clone)")
{
Destroy(collision.gameObject);
}
}
The output 'hit' simply refers to a collision being detected, not the object being successfully destroyed (incase it came to mind that that was the problem).
Thanks for any help.
Hey, is the debug logging "hit"? or is it just not doing anything? If not I would make sure that there is a rigidbody attached the the object and that isTrigger is checked.
If it is logging collisions but not registering the if statement perhaps adding an extra bit to the
Debug.Log("hit"+collision.gameObject.name)
and checking to make sure you have the right name.
I would also suggest that maybe giving your prefab a tag and checking that would be better than checking the name
collision.gameObject.tag == "Tag"
Sorry it's just a bunch of suggestions and not a solid answer :) hope it helps in some way.
So yeah I added the name to the output, and sure enough it is colliding with the lasagnyi(Clone), which is all fine.
I've had a look into tags but can't figure it out... to add a tag would I click on the lasagnyi prefab and there's a dropdown menu (with default Untagged)... but the options it offers are like 'Player' and '$$anonymous$$ain Camera'... which would I use, and if I were to add my own, how would I do that? It just takes me to a tags page..
Thanks for the help
To add a tag:
You click the drop down and click "Add Tag..."
Then at the top of the list you will see an arrow with the word Tags next to it
Click the arrow
You will then see "size" "Element 0"
Click on the "Element 0" field and type in what you want your tag to be
You can then go back to your prefab and select this new tag to apply it to you prefab
You can add in as many elements as you want here for future reference.
Also I would put a debug into the if statement just to check if it is actually working since the names match.
Example:
void OnCollisionEnter(Collision collision)
{
Debug.Log("hit");
if(collision.gameObject.name == "lasagnyi(Clone)")
{
Debug.Log("match");
Destroy(collision.gameObject);
}
}
If the debug displays from there, then there is most probably an issue with the Destroy function.
I don't see anything wrong with these scripts, they should work fine. It may have something to do with the player movement controller script. How are you re-positioning him?
Also, just go to Edit > Project Settings > Tags. Once that's open in the inspector, hit the drop down arrow next to Tags, then add your own Tags in the "Element 0: " entry, you can add more in element 1, 2, 3 etc.
If it works fine the first time, then the "redo" code is probably the issue. For all we know, just the model is brought back, leaving behind the collider.
Your answer
Follow this Question
Related Questions
Problem Detecting 2D Collisions 1 Answer
Call a Void on Collision 2 Answers
Sounds triggering too fast 4 Answers
My weapon takes damage. help... 1 Answer
Missing GameObject and Null do not test the same, but how do I test for missing? 6 Answers