1
votes

My bullet can not detect the collider and raycast can not detect the collision. It's very weird since the only way to get a message on the console is whenever I shoot bullets within the range of my terrain(either on or above), I instantly get "Terrain" printed on my console, but the raycast cannot detect any other objects and print anything, and if I go out of the range and shoot at a sphere, nothing gets printed. Everything in my game has a collider except the bullet.

Thanks!

Here's an image of my game game.

void Update () {
    if (Input.GetKey(KeyCode.KeypadEnter) && counter > delayTime)
    {
        Instantiate(bullet, transform.position, transform.rotation);
        counter = 0;
        RaycastHit hit;

         if (Physics.Raycast(transform.position, -Vector3.up, out hit))
        {
               Debug.Log(hit.collider.gameObject.name); 
        }
    }

    counter += Time.deltaTime;
}
1
Is your raycast detecting anything? Can you print out the name of the object it hits to check?Serlite
No. If I I only have Debug.Log inside the first if condition, it does not print anything in the console.781850685
Try using Debug.DrawLine() or Debug.DrawRay() to figure out where the raycast is going - this may give you a better idea of where the issue lies (whether it's a failure to detect an object, or a misdirected ray).Serlite
With "first if condition" you mean Input.GetKey...? If so, do you really want the num pad enter on held down ("auto fire")? Maybe give if(Input.GetButtonDown("Jump") && counter > delayTime) a try (that would be spacebar).Gunnar B.
You simply use Invoke to make things happen in 3 seconds.Fattie

1 Answers

0
votes

Add a collider to your bullet prefab. You should have tags on your enemies or other destructible objects. Use OnCollisionEnter OR OnTriggerEnter. For enemies, I prefer to use OnCollisionEnter for the most part.

 void OnCollisionEnter(Collision collision){//Assuming bullet touches enemy

   if(collision.tag=="Bullet"){

    // insert your code here for damage
  }
  }

As far as your RayCast, I'd do something like this:

   Vector3 fwd = transform.TransformDirection(Vector3.forward) * 3; // length of ray 
   //forward-facing. ( * 3 is equal to 3 units/Meters )
    Debug.DrawRay(transform.position, fwd, Color.red); // Can Make any color 

        if (Physics.Raycast(transform.position, fwd, out hit))
        {


            print(hit.collider.gameObject.name);

        }

See, the reason I'd use collision detection is that as long as your enemy is tagged you'll make contact. Otherwise, your raycast should detect contact and you can still set collision damage or whatever.