Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast 3d not casting anything

Discussion in 'Scripting' started by Hishicorne, Jun 16, 2018.

  1. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Hello !

    I have a little problem on my game right now, i'm trying to add a raycast shooter and here is my code :

    Code (CSharp):
    1. void Update () {
    2.         // If the player is shooting
    3.         if (Input.GetButtonDown ("Fire1")) {
    4.             source.PlayOneShot(shotSound); // Play a sound
    5.             StartCoroutine("shootAnimation"); // Play the animation
    6.             Shoot (); // And call the shoot function
    7.         }
    8.     }
    9.  
    10.     void Shoot () {
    11.         RaycastHit hit;
    12.         if (Physics.Raycast (camera.position, camera.forward, out hit, weaponRange)) {
    13.             Debug.Log ("hit" + hit.transform.name);
    14.         }
    15.     }
    the problem is that i never have any log in my console about what the raycast hit. All the object do have a box collider (3d obviously), but it still don't works. I tried moving my camera (wich is in the middle of my character that don't have box collider but only a character controller), and even OUTSIDE of him, it do not work... so i'm actually clueless about what to do haha !

    I know it's a beginner problem but thank by advance for your help !
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Try using DrawRay to see if that helps debug the problem. For example, does it show that an object is actually in the line of fire?
     
  3. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    I just tried it and it does show that an object is in the line of fire (it goes through it), but still no log :(
     
  4. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Ok i'm actually a huge idiot, because i just desactivated all debug messages in my console so OBVIOUSLY they did'nt show up. Except this everything works fine. So sorry for this, it was really the most dumb problem i encountered on unity so far haha...
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Nice. Glad to hear you figured it out. :)