0
\$\begingroup\$

I'm trying to get the actual position a raycast from a controller hit's a sphere gameobject.

The raycast is cast from the controller (gear vr controller) out in the forward direction when the trigger is pressed, but when I test the value of the hit.point, I get varying values depending on what sphere I hit.

Shouldn't the hit point be roughly the same if I say always hit the sphere's more or less dead center?

Here's the code I use to cast the ray, this runs when I click the trigger of the controller:

if (OVRInput.GetDown(LaserInputModule.instance.trigger) && triggerdown == false)
    {
        triggerdown = true;
        RaycastHit hit;
        if (Physics.Raycast(LaserInputModule.instance.helperCamera.transform.position, LaserInputModule.instance.helperCamera.transform.forward, out hit))
        {
            Debug.Log(hit.transform.name + " got hit at: " + transform.InverseTransformPoint(hit.point) );
        }
        else
        {
            Debug.Log("PlayerMissed");
        }
    }

LaserInputModule.instance.helperCamera is basically the controller the player has in their hand.

When I test the values in logcat, for the first picture, I get back what I assume to be correct values 1st pic Sphere(Clone) got hit at: (-0.2, 0.1, -0.5) (so x,y are roughly in the center of the sphere)

But when I do the same for another sphere (2nd Pic)enter image description here, I get back some spurious values Sphere(Clone) got hit at: (-7.2, 2.0, -0.4)

2nd pic

\$\endgroup\$
1
\$\begingroup\$

Ahh face palm moment, you were right, I had it running on the update method for every sphere. So it was giving me values for every one of the 12 spheres, I just didn't see it in the logcat as it was spamming it.

I've moved this logic off the sphere's into it's own gameobject and works great. thanks for your help.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ In future, make sure your question includes relevant details like which object(s) in the scene are running a particular code snippet, and in which method. \$\endgroup\$ – DMGregory Mar 19 at 16:15
0
\$\begingroup\$

It looks like you're trying to output the position in the sphere's local coordinate system, but you have a typo, so you're using the raycaster script's local coordinate system instead.

if (Physics.Raycast(LaserInputModule.instance.helperCamera.transform.position, LaserInputModule.instance.helperCamera.transform.forward, out hit))
{
    //                 ↓ This is the transform of the sphere you hit.
    Debug.Log(hit.transform.name + " got hit at: " + transform.InverseTransformPoint(hit.point) );
    //                                                 ↑  This is the transform of the object
    //                                                    that this script is running on.
}

If you want to use the hit object's local coordinates, make sure to use hit.transform, not just transform.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Hi thanks for the query, yes they are the same transform. This script runs on the sphere gameobject, but I'll change it anyway - good spot :) \$\endgroup\$ – Chris banwell Mar 19 at 15:06
  • \$\begingroup\$ Writing a raycast like this on the raycast target itself is a strange choice. You have no guarantee in the code we can see that the object that was hit is "this" object. Can you edit your question to give us more context about what you're trying to accomplish? \$\endgroup\$ – DMGregory Mar 19 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.