- Home /
The question is answered, right answer was accepted
Collision being ignored
I am having trouble in making a cillinder show a message when it hits an Enemy. I was seeing a video about Counter-Strike when i thought "Oh, it would be a nice idea to use that same system!" Most FPS games use an invisible line that when find an enemy, activate a trigger that will make the enemy be hit instantly.
This makes impossible for the enemy to dodge hits, but it is very easy to make and pretty nice by the way. It would reduce my things to do list for now (i am developing the enemy) in my FPS.
The problem is that something is wrong here... I don't know if it is bcs i have been studying literally 3 different programming languages and i am kinda forgotting the basic things sometimes, but something is wrong with my script.
I made a simple script for now that WAS SUPPOSED to detect any gameobject that collide with my cillinder. When it collides, the script will detect if it is an enemy by searching if it is tagged as "Enemy". (Javascript)
function OnCollisionEnter (Aim : Collision)
{
if(Aim.gameObject.tag == "Enemy")
{
Debug.Log ("TARGET HIT!");
}
}
The problem is that basically... It doesn't show the log! Why? I also changed it to just (Collision: Collision) and nothing changes, the log isn't shown. My cillinder does have a collider, the enemy does have a collider, i don't understand! Is it because i have two cameras or something? Also my cillinder is passing through objects. =(
Edit: Here are two screen shots. One is of my current test cillinder, and the other is from the Enemy. Hope it helps!
Answer by tanoshimi · Nov 04, 2016 at 08:59 PM
You need a rigidbody component attached to your enemy.
"With normal, non-trigger collisions, there is an additional detail that at least one of the objects involved must have a non-kinematic Rigidbody" https://docs.unity3d.com/Manual/CollidersOverview.html
Okay, i didn't considered it. It works =)
But now when it hits my enemy he is knocked away...
Yes, because that's what happens when two objects collide. $$anonymous$$ake the mass of your projectile smaller if you want it to apply less force.
By the way, do you know how to ignore a tag when using OnCollision? Something like:
function OnCollisionEnter (Collision : Collision)
{
if (Collision.gameObject.tag == "Aim")
{
Ignore.Object
}
}
Something lik ethis.
Answer by ToasterKyle · Nov 04, 2016 at 08:17 PM
I don't see my original answer so I'm posting a new one as a sort of edit to my original(if you can even see it) but also try using Aim.collider.tag instead of Aim.gameObject.tag. Not sure what the difference is but once I had a problem similar to yours and I believe it was fixed by changing that. It was a while ago though
Answer by ToasterKyle · Nov 04, 2016 at 08:17 PM
Is the Cylinder a trigger? Also, try just Debug.Log outside of the if statement so that when the enemy hits anything it will log it just for a test, if or when that works you can delete it and do further testing
I already tried with trigger options and it doesn't change anything. Also, as far as i know you don't need to use triggers when you are using OnCollision
. (I use it in other projects without triggers and it works fine) And even with the Debug.Log
outside the if statement, it still doesn't work. The command Debug.Log
works normally, which prove that it is NOT a bug/glitch. The problem here is that the function isn't being called or something like this, but i don't know why. $$anonymous$$y cillinder clearly is IGNORING the other colliders, and this is making the script bug, but i don't know why and how to solve it =\
Follow this Question
Related Questions
Reference to another script not working, not throwing any errors 0 Answers
SetActive messes up everything? 1 Answer
Collision with a coin 0 Answers
If two PlayerPrefs are on? 2 Answers
Character Controller NOT COLLIDING. 0 Answers