1
\$\begingroup\$

I'm using unity to raycast a line from the center of my camera viewport to a world position (yellow line).

I also want to cast a ray from the center of my character to this same point (blue line).

I have tried two methods to get this to work, however neither has worked.

The two methods are the following:

        if (Input.GetKey (KeyCode.Mouse0)) { //if left click is held down

        Ray crosshairPoint = viewCamera.ViewportPointToRay (new Vector3(0.5f, 0.5f, 0.0f));
        Vector3 crosshairEnd = (crosshairPoint.origin + crosshairPoint.direction * 15);
        Debug.DrawRay (crosshairPoint.origin, crosshairPoint.direction * 20, Color.yellow, Time.deltaTime, false);
        Vector3 intersect = crosshairPoint.GetPoint (20);
        Debug.DrawRay (Vector3.zero, intersect, Color.red, Time.deltaTime, false);

        Debug.DrawRay(_charFace.position, intersect, Color.cyan, Time.deltaTime, false);

    }

The red line being drawn is a line from the origin, which has no problem intersecting my camera ray line. The one from my character just refuses to point at the given point.

The second method is the same as above, but using vector math (origin + direction * distance) to get the same point.

Any ideas why the ray from my character won't point the right way, but one from the origin would? Something to do with world vs. local coordinates?

Thanks![enter image description here]1

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

I ended up using Physics.linecast from the _charface.transform.position to the intersect point.

\$\endgroup\$

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.