RayCast goes to wrong side ?

Hi
I wrote a simple raycast but it goes to wrong side. what is wrong i don’t know. can any one fix it ?

    public GameObject weaponPlace;
    public bool FireON;

    void Start () {

    }
   
    void Update () {

        RaycastHit hit;

        Ray gunray = new Ray(weaponPlace.transform.position, Vector3.forward);
        Debug.DrawLine(weaponPlace.transform.position, Vector3.forward);

        if (Physics.Raycast(gunray, out hit, 200)) {
            if (hit.collider.tag == ("enemy")) {
                FireON = false;
            }
            else {
                FireON = true;
            }
    }

    }// end update

try using Vector3.forward * -1

How do you know it’s going to the wrong side? Is it because of Debug.DrawLine?

If so, note that Debug.DrawLine draws a line from point A to B in world space. Vector3.forward is (0,0,1), so you’re always drawing a line to (0,0,1).

Building on what @BlackPete said above, you might want something like:

Debug.DrawLine(weaponPlace.transform.position,
                    weaponPlace.transform.position + weaponPlace.transform.forward);


As you see, white line is the drawLine

I tired that but nothing change.

I’m trying to use raycast to detect enemy object at front of my cannon, to see the raycast I drawLine. Look at the image above. back to your question. because the Bool FireON not working. I change the script now to make it more clear. but still the FireON bool not working !!

    void FixedUpdate()     {
        Vector3 fwd = weaponsPlace.transform.TransformDirection(Vector3.forward);
        Debug.DrawRay(transform.position, fwd * 10 , Color.green);

        RaycastHit hit2;
        if (Physics.Raycast(weaponsPlace.transform.position + (fwd * 3), fwd, out hit2,100)) {
            if(hit2.collider.tag == ("enemy")) {
                FireON = false;
            }
            else{
                FireON = true;
            }
        }
    }

Maybe. Could you please look at this script and tell my why the bool FireON no working ?

    void FixedUpdate()     {
        Vector3 fwd = weaponsPlace.transform.TransformDirection(Vector3.forward);
        Debug.DrawRay(transform.position, fwd * 10 , Color.green);

        RaycastHit hit2;
        if (Physics.Raycast(weaponsPlace.transform.position + (fwd * 3), fwd, out hit2,100)) {
            if(hit2.collider.tag == ("enemy")) {
                FireON = false;
            }
            else{
                FireON = true;
            }
        }
    }

You gotta break it down into constituent steps, take it one step at a time:

  • spawn a sphere at the position you’re raycasting from - is it where you think it is?
  • spawn another sphere in the direction you think that ray is going… is that what you expect?
  • attach the debugger, put a breakpoint on line 7: does the raycast even hit at all?
  • with that breakpoint on line 7, inspect hit2.collider and see what its name is, tag, etc.

The easiest way to do the first two parts above is to put a Debug.Break() statement at the start of your FixedUpdate() call and then the editor will be frozen so you can go and find the two new spheres you created at those positions, then you can stop before you get 50,000 spheres everywhere.

Yes, you right. I solved. there is no error with the script except that the raycast line “6” start a little be away from it position. Now all things is good.

1 Like