Raycast from rotation

Hi Guys,

I want to raycast from a abstract rotation with no gameObject attached, from a random position. How do I do this?

I currently have this code:

public GameObject FindClosestFacingTarget()
    {
        Vector3 playerPos = GetComponent<GameWorldData>().GetCameraPosition();
        Vector3 playerRot = GetComponent<GameWorldData>().GetCameraRotation();

        RaycastHit hit;
        if (Physics.Raycast(playerPos, playerRot, out hit))
        {
            Debug.DrawRay(playerPos, playerRot, Color.black, 5.0f);
            Debug.Log("HIT" + hit.collider.name + playerRot);
        }
        else
        {
            Debug.DrawRay(playerPos, playerRot, Color.red, 5.0f);
            Debug.Log("NO HIT" + playerPos + playerRot);
        }

        return null;
    }

I can’t use the Transform of the camera itself, as it’s IsActive = false at the time of this script being called.

No matter what I do, it just seems to fire up. I’ve tried switching the Y to the X with a temporary Vector3, and using that instead, and while that doesn’t fire up anymore… It just fires right…

Here’s the log:

NO HIT(160.4, 91.8, -302.8)(0.0, 332.1, 0.0)
UnityEngine.Debug:Log(Object)

What does your GetCameraRotation return?

It literally passes this:

Vector3(0.0, 332.1, 0.0)

I’ve tried switching the Y float to an X axis in a temporary Vector3 and tried using that… Although it then faces right all the time, instead of facing up…

The second argument of Raycast is a direction, not a point. In your case, you’re raycasting directly up from wherever the player is.

If you want what direction the camera is facing, grab the camera’s transfrom and get it’s .forward, and use that to raycast with.

The raycast is firing exactly like it should in exactly the direction that it should, given those coordinates. What isn’t working how you think it should work? You gave us a script that has everything except how the rotation is obtained in the first place. If you think it’s not firing in the correct direction, then show us how it came up with the idea to fire in that direction.

Baste
That logic sounds right. However I really want to learn how to send abstract rotations to Raycast without needing some objects Transform.forward/right/some other direction.

However upon trying that, because the object is no longer active I can’t use it’s transform:

// Attached to GameWorldObject
    public Transform GetCameraTransform() { return camTransform; }
    public void SetCameraTransform(Transform trans) { camTransform = trans; }

// Attached to cameras
GameObject gwObj;
                if (gwObj = GameObject.Find("_GameWorldData"))
                {
                    if (bwObj = gwObj.GetComponent<GameWorldData>())
                    {
                        // Pass camera's rotation for other camera's to collect
                        bwObj.SetCameraRotation(gameObject.transform.rotation.eulerAngles);
                        bwObj.SetCameraTransform(gameObject.transform);
                    }
                    else
                    {
                        Debug.Log("CameraCoords::OnDisable - Cannot send rotation to GameWorldData::mainCameraRot! GameWorldData, does not exist!");
                    }
                }
                else
                {
                    Debug.Log("CameraCoords::OnDisable - Cannot send rotation to GameWorldData::mainCameraRot! GameWorldData, does not exist!");
                }
            }


// Class checking
public GameObject FindClosestFacingTarget()
    {
        Vector3 playerPos = GetComponent<GameWorldData>().GetCameraPosition();
        Vector3 playerRot = GetComponent<GameWorldData>().GetCameraRotation();
        Transform camTrans = GetComponent<GameWorldData>().GetCameraTransform();

        RaycastHit hit;
        if (Physics.Raycast(playerPos, camTrans.forward, out hit))
        {
            Debug.DrawRay(playerPos, playerRot, Color.black, 5.0f);
            Debug.Log("HIT" + hit.collider.name + playerRot);
        }
        else
        {
            Debug.DrawRay(playerPos, playerRot, Color.red, 5.0f);
            Debug.Log("NO HIT" + playerPos + playerRot);
        }

        return null;
    }

When the camera is disabled, it sends it’s data to GameWorldData. Here’s the script which does that.

    void OnDisable()
    {
        // Outside of instantiate/clean up phase
        if(bActiveStatus)
        {
            // Make sure object exists
            if (bwObj)
            {
                // Pass camera's rotation for other camera's to collect
                bwObj.SetCameraRotation(gameObject.transform.rotation.eulerAngles);
            }
            else
            {
                GameObject gwObj;
                if (gwObj = GameObject.Find("_GameWorldData"))
                {
                    if (bwObj = gwObj.GetComponent<GameWorldData>())
                    {
                        // Pass camera's rotation for other camera's to collect
                        bwObj.SetCameraRotation(gameObject.transform.rotation.eulerAngles);
                    }
                    else
                    {
                        Debug.Log("CameraCoords::OnDisable - Cannot send rotation to GameWorldData::mainCameraRot! GameWorldData, does not exist!");
                    }
                }
                else
                {
                    Debug.Log("CameraCoords::OnDisable - Cannot send rotation to GameWorldData::mainCameraRot! GameWorldData, does not exist!");
                }
            }
        }
    }

The GameWorldData just has this:

    public Vector3 GetCameraRotation() { return viewCamRot; }
    public void SetCameraPosition(Vector3 newPos) { viewCamPos = newPos; }
    public void SetCameraRotation(Vector3 newRot) { viewCamRot = newRot; }

Okay, I think I understand what you need. Try “Quaternion.Euler(playerRot) * Vector3.forward” and be sure to put them in that order (a Quaternion times a Vector3 returns a Vector3, but a Vector3 times a Quaternion will give an error).

1 Like

Perfect thanks!