- Home /
I want to get a projectile to return the surface tag but it returns untagged.
Basically i want my projectile to mark the map if its tagged as LevelParts but the problem is that the projectile isnt even detecting the tags on the level. A raycast version works perfectly but this one does not.
The code is obviously detecting collisions but not correctly. What have i done wrong?
function OnCollisionEnter(collision : Collision)
{
for(var contact : ContactPoint in collision.contacts)
{
Debug.Log("This colliders tag is named: " + collision.contacts[0].thisCollider.transform.tag);
}
}
I have tried even changing "ollision.contacts[0].thisCollider.transform.tag" to "contact.thisCollider.tag" and other things.
If its any more help this is the actual piece of code i am actualy wanting this to work on.
function OnCollisionEnter (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if(decalHitWall && contact.thisCollider.tag == "LevelParts")
{
Instantiate(decalHitWall, contact.point + (contact.normal * floatInFrontOfWall), Quaternion.LookRotation(contact.normal));
Destroy(gameObject);
}
}
}
I'm a bit confused about what you are trying to do here. Your first code makes no sense to me since you loop through all the contact points, but you always only print out the data for the first contact.
The second body is also a bit confusing since you use 'thisCollider' rather that 'otherCollider'. If this script is on the projectile and if it is trying to mark what it hit, then I think you want 'otherCollider.tag' for your test.
Put it simply i want the code to:
Detect collision with any object that has the tag "levelParts"
Place a decal on the contact.point and rotate it to the contact.normal.
Destroy the bullet.
The bullet is basically a sphere with a box collider attached.
Answer by KellyThomas · Jan 17, 2014 at 11:28 PM
Try:
function OnCollisionEnter(collision : Collision)
{
for(var contact : ContactPoint in collision.contacts)
{
Debug.Log("The other collider's tag is named: " + contact.otherCollider.tag);
}
}
Answer by Jamoy1993 · Jan 18, 2014 at 12:58 AM
function OnCollisionEnter (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (decalHitWall && contact.otherCollider.tag == "LevelParts")
{
Debug.Log("Tag = " + contact.otherCollider.tag);
}
}
}
This was what i was looking for.
THANKS ROBERT your input heped me by mentioning otherCollider.
Answer by fuzzysmurf · Jan 18, 2014 at 12:00 AM
If their is only one GameObject interacting w/ these contact points, you can add them all to a seperate layer, and use Physics.OverlapSphere to detect them all first. then you can check for the tag afterwards. guaranteed it will work.
Collider[] parts;
parts = Physics.OverlapSphere(this.transform.position, 4); //you can change the parameters if you're using layers.
foreach (Collider part in parts)
{
if(part.gameObject.tag == "levelParts")
{
//do stuff
}
}
of course if you do this, you will have to activate it upon collission, or on trigger.
Your answer
Follow this Question
Related Questions
Problem with Rigidbody control script! (collision with perpendicular walls problem) 1 Answer
Rigidbody (2D) stays in fixed angle even when colliding 1 Answer
Offsetting player position from a moving Rigidbody. 0 Answers
OnCollisionEnter and rigidbody problem 1 Answer
Rigidbody slightly intrudes another one while pushing it 1 Answer