- Home /
Camera.ViewportToWorldPoint giving different values
I am having an issue where ViewportToWorldPoint is returning different values in different parts of the code.
The values are correct when code is run in the OnDrawGizmosSelected method:
void OnDrawGizmosSelected() {
Vector3 topLeft = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 1, GetComponent<Camera>().nearClipPlane));
Vector3 topRight = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 1, GetComponent<Camera>().nearClipPlane));
Vector3 bottomLeft = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, GetComponent<Camera>().nearClipPlane));
Vector3 bottomRight = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 0, GetComponent<Camera>().nearClipPlane));
Debug.DrawLine (topLeft, target.transform.position, Color.red);
Debug.DrawLine (topRight, target.transform.position, Color.red);
Debug.DrawLine (bottomLeft, target.transform.position, Color.red);
Debug.DrawLine (bottomRight, target.transform.position, Color.red);
Gizmos.color = Color.yellow;
Gizmos.DrawSphere(topLeft, 0.1F);
Gizmos.DrawSphere(topRight, 0.1F);
Gizmos.DrawSphere(bottomLeft, 0.1F);
Gizmos.DrawSphere(bottomRight, 0.1F);
}
Yet, when the same (for the most part) code is called in another method in the same script, I get different values:
void HandleCollision()
{
Vector3 pos = transform.position;
transform.position = _standardPosition;
Vector3 topLeft = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 1, GetComponent<Camera>().nearClipPlane));
Vector3 topRight = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 1, GetComponent<Camera>().nearClipPlane));
Vector3 bottomLeft = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(0, 0, GetComponent<Camera>().nearClipPlane));
Vector3 bottomRight = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(1, 0, GetComponent<Camera>().nearClipPlane));
Vector3 center = GetComponent<Camera>().ViewportToWorldPoint(new Vector3(.5f, .5f, GetComponent<Camera>().nearClipPlane));
transform.position = pos;
RaycastHit rayhit;
List<RaycastHit> hits = new List<RaycastHit>();
Debug.DrawLine (topLeft, target.transform.position, Color.blue);
Debug.DrawLine (topRight, target.transform.position, Color.blue);
Debug.DrawLine (bottomLeft, target.transform.position, Color.blue);
Debug.DrawLine (bottomRight, target.transform.position, Color.blue);
Debug.DrawLine (center, target.transform.position, Color.blue);
Here is an image of the issue (red lines are from OnDrawGizmos, blue are from HandleCollision), it seems to be calculating as if the camera is at (0,0,0):
Does anyone know what may be causing the difference? HandleCollision is called during LateUpdate().
Never$$anonymous$$d, had a brain problem... transform.position = _standardPosition; was causing the issue, because _standardPosition was (0,0,0)... oops! I will mark as answered once it's approved.
Your answer
Follow this Question
Related Questions
How to open the resources from a downloaded game to add another viewpoint? 0 Answers
Keep Aspect Ratio like in Unity Editor 2 Answers
Switching to the player camera 1 Answer
Dedicated Cameras? 1 Answer