- Home /
Collider not registering hits?
Heyo, I've been trying to create a prefabed Projectile that is instantiated and shot at a player to deal damage. The problem is the shots don't seem to be registering when they hit the player.
The code for the projectile:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile1 : MonoBehaviour
{
public GameObject player;
public void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void OnCollisionEnter(Collision col)
{
if ( col.gameObject.tag == "Player")
{
player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
player.GetComponent<HopeHealth>().TakeDamage();
Debug.Log("player has been hit");
}
}
}
Im closer to a beginner so I'd appreciate an answer not too complicated Lol. Thanks in advance for the help. :)
Thank you to @OutOfRam and @Ifran-unity for your quick responses!
Did you make sure that the tag is actually the same? (keep in $$anonymous$$d that it's case sensitive). Also, did you make cure that the collider is not set as isTrigger?
Answer by OutOfRam · Sep 26, 2017 at 04:29 AM
Hey there,
The issue here is that you are attempting to grab the gameobject component from a collision event. when you call "void OnCollisionEnter(Collision col)" the parameter you are passing in is "Collision", not "Collider". the collision parameter holds info like the point of contact the force of the hit, the collider hit, and some other things. what you need to do is change the subsequent if statement...
if ( col.gameObject.tag == "Player")
to
if ( col.collider.gameObject.tag == "Player")
Hope this helps!
@OutOfRam Thank you too for your reply. I tried this:
void OnCollision(Collision other)
{
Debug.Log("hit");
if (other.collider.gameObject.tag == "Player")
{
player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
player.GetComponent<HopeHealth>().TakeDamage();
Debug.Log("player has been hit");
}
}
To no avail :/ In fact it's not even registering my debug.log.
Hello,
I did not at first glance realise your game was 2D. You are currently using the Unity 3D collision method try this
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("hit");
if (col.collider.gameObject.tag == "Player")
{
player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
player.GetComponent<HopeHealth>().TakeDamage();
Debug.Log("player has been hit");
}
}
Answer by irfan-ayub · Sep 26, 2017 at 04:45 AM
There are multiple possibilities.
===>> You must have a RigidBody component on any one of two bodies you are trying to collide. check in the inspector that you have a RigidBody component added to any one of these bodies.
or it would be 2D if you are using 2D bodies.
===>> You might have Enabled onTrigger flag in the collider component on any of your bodies, for detecting collisions you must have it off. (This flag is to trigger only and the bodies will cross each other without any collision.)
===>> Your Projectile might be moving very fast and the collider component is not able to detect the collision on that particular frame or time. try making your collider on player bigger or slowing down the speed of your projectile.
Hope It Helps.. Happy Coding and Development.. :) and Don't Lose your hope..
@irfan-unity, Thank you as well, I checked on all three solutions and still nothing.
Both the projectile and the player have rigidbody2Ds
Neither has the Is Trigger flag enabled
Halved the speed to see the same results
you are working in 2D and the function you are using is OnCollisionEnter().
for 2D you must use OnCollisionEnter2D(Collision2D other). See this for details.
Answer by Vilhien · Sep 26, 2017 at 12:59 AM
I'm semi new myself (two weeks). My best guess is you have no reference to the script that is trying to tag HopeHealth right off the bat.. Try this.
Below your public callout to the GameObject player;
HopeHealth hope;
That will reference your script. Then right below where you call out he player in start. try this..
hope = player.GetComponent(HopeHealth);
Hey @Vilhien, thanks for your response, see the issue doesn't lie with the actions called on collision, because the collision doesn't seem to even register. Hence why I added the debug.log to the beginning of the event.
Answer by Agent27765 · Sep 26, 2017 at 02:39 AM
Hello I find myself as a beginner although I have been programming for 3 months or so. In this case this is possibly a different way I would do it. Its possible you forgot to tag the object with the EXACT same tag as "Player" If that is not the case. Try something like this using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Projectile1 : MonoBehaviour
{
public GameObject player;
public void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
}
void OnCollisionEnter(Collision other)
{
if ( other.gameObject.tag == "Player")
{
player.GetComponent<HopeHealth>().HopeDamageAmount = 20;
player.GetComponent<HopeHealth>().TakeDamage();
Debug.Log("player has been hit");
}
}
}
See the change with other in the void OnCollisionEnter(Collision other) and also other.gameobject.tag. I believe that I have had the same problem with that a while back and I believe that is the way I have fixed it. I hope you found this helpful!
Hey @Agent27765, thank you also for the response. I tried both solutions and still nothing! I'm losing hope!
Your answer
Follow this Question
Related Questions
Cannot change the value of this integer that I use for array,Cannot change the value of this one. 0 Answers
Standard Asset FP Controller not moving 0 Answers
When I enable certain scripts on my player, the transform falls through the map. 0 Answers
how do i add a offset to my sword rotation? 2 Answers
Help with a simple procedural generation 0 Answers