- Home /
Raycast not reporting what it hit
The issue, I believe, rests on line 46 of my code. I raycast out, and in this version of the code:
using System.Collections;
public class FPscript : MonoBehaviour {
//public GameObject untitled;
float bulletImpulse = 100f;
bool test = false;
RaycastHit hitt;
void Start (){
}
void OnControllerColliderHit(ControllerColliderHit hit){
if (hit.gameObject.tag == "Key") {
test = true;
}
}
void Update (){
if (Input.GetKey(KeyCode.Escape)){
Application.Quit ();
}
//if(test == true){
//if( Input.GetButtonDown("Fire1") ) {
//Camera camm = Camera.main;
//GameObject thebullet = (GameObject)Instantiate(untitled, camm.transform.position + camm.transform.forward, camm.transform.rotation);
//thebullet.rigidbody.AddForce( camm.transform.forward * bulletImpulse, ForceMode.Impulse);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if( Input.GetButtonDown("Fire1") ) {
if (Physics.Raycast(transform.position, fwd, 10)){
// if(hitt.collider.gameObject.tag == "Enemy"){
Debug.Log("Object Hit");
// }
}
}
}
}
I get a Debug.Log report, but when I take out the comments on the line that reads // if(hitt.collider.gameObject.tag == "Enemy"){
, nothing happens no matter what. I'm a little stumped, as I have triple checked and this seems to be the correct way to test what you hit. Any suggestions from anyone?
there is nothing on line 46. It's a blank line -_-
On that note. How do you know it's hitting something. The raycast function always returns a bool, so that is NOT the problem.
It was 46 in $$anonymous$$onoDevelop. It's 45 in the webpage. Either way, I have several problems that I have to iorn out. It's gonna take awhile. Thanks for the input, though.
Also, some other interesting things are happening. When I comment out line 45 in the code above, I get the output "Object Hit" when I fire at trees or houses, but when I fire at the enemies on the game, I still get nothing, even though it is still a game object. I've double checked my tags and layers, and everything is on the same layer and enemies are tagged "Enemy". Very confusing turn of events for this noob.
C# EXA$$anonymous$$PLE:
RaycastHit hit;
void Update()
{
if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity))
{
print(hit.collider.gameObject.name);
}
}
Are you sure your ray is hitting the enemies? 10 meters is not very far for a Raycast you should really crank up the range to make sure you can shoot far enough to hit the enemies.
Also you might need to double check your collider set up on the enemies. $$anonymous$$ake sure the biggest or preferably only collider object on them (for troubleshooting purposes) is the one with the tag "Enemy". If you have child objects on the enemy it's possible you are hitting a child object without this tag?
You might also want to try using .name on the enemies ins$$anonymous$$d of .tag just to see if you specify the name of the collider object on enemy if it would work.
I think you need to provide some more info though for help if it still doesn't work.
Answer by RyanZimmerman87 · Sep 07, 2013 at 08:22 AM
You forgot to include hitt in your raycast.
You need:
if (Physics.Raycast(transform.position, fwd, out hitt, 10))
Your answer
Follow this Question
Related Questions
How to raycast hit empty space? 1 Answer
C# Raycast is only hitting one item and then stopping 1 Answer
Debug.log not working with raycast 1 Answer
How to register if my Raycast hits nothing(Solved) 2 Answers
Logic Required 1 Answer