- Home /
Orthographic position to Perspective position
Hi everyone,
The question might be a little bit tricky, but I hope someone can help me.
I have a GUI button, rendered by a GUI camera, which is set on orthographic view. I have another camera, the main camera, which is set on perspective view.
When I dragg my GUI button, it gives my character a position to aim. When I release the dragg, an arrow is instantiated in direction of the GUI button direction.
But, as you can see in the screenshot below, the orthographic position and the perspective position are not the same at all, resulting on an approximative aiming.
So, is there anyway to make both position coincide?
A little bit of my code, regarding to the "dragging" and instantiate part :
void OnMouseDrag()
{
Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, aimTarget.z);
Vector3 currentPosition = guiCamera.camera.ScreenToWorldPoint(currentScreenPoint) + offset;
transform.position = currentPosition;
if (startMousePosition != Input.mousePosition)
{
aimController.GetComponent<AimController>().enabled = true;
if (transform.localPosition.x < 0)
{
playerController.moveDirAccess = -1;
}
if (transform.localPosition.x > 0)
{
playerController.moveDirAccess = 1;
}
}
else
{
aimController.GetComponent<AimController>().enabled = false;
}
}
void OnMouseUp()
{
GameObject.Find("AimButton").transform.localPosition = startlocalPosition;
aimController.GetComponent<AimController>().enabled = false;
newArrow = (GameObject)Instantiate(Resources.Load("Arrow"), arrowPosition.transform.position, arrowPosition.transform.rotation);
newArrow.rigidbody.velocity = arrowPosition.transform.TransformDirection(arrowForce, 0, 0);
playerController.audio.PlayOneShot(playerController.arrowSound);
newArrow.name = "Arrow";
Physics.IgnoreCollision(newArrow.collider, collider);
if (playerCharacter.transform.rotation == faceLeft)
{
arrowPosition.transform.rotation = new Quaternion(0.0f, 1.0f, 0.0f, 0.0f);
}
else if (playerCharacter.transform.rotation == faceRight)
{
arrowPosition.transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, -1.0f);
}
playerController.speed = 12;
playerController.jumpHeight = 16;
backgroundAimButton.SetColor("_Color", new Color(1, 1, 1));
playerController.animator.SetBool("Bending", false);
}
What you can do is to get either the viewport or screen coordinate of the button using the GUI camera. Then use that coordinate and the perspective camera to get the world coordinate to aim at.
Hi robertbu,
Thank you so much for your help. I managed to make it work by following your advice!