- Home /
Question by
jatmdm · Nov 13, 2013 at 01:37 PM ·
raycastmousepositionunity 4.3function update4.3
Unity Mouse Position Raycasting from Point
I just implemented a mouse control for a 2D Game in Unity 4.3, and I'm having some troubles with Raycasting. This code returns the correct Atan2 based on the mouse position, but only if the camera position is at the origin:
function Update(){
var ray : Ray = cam.main.ScreenPointToRay(Input.mousePosition);
mousePos = new Vector2(ray.origin.x, ray.origin.y);
gunRot = Mathf.Atan2((mousePos.y + Player.transform.position.y * 1f), (mousePos.x + Player.transform.position.x * 1f));
}
I recently tried again with this code:
var ray : Ray = cam.main.ScreenPointToRay(Input.mousePosition);
mousePos = new Vector2(ray.GetPoint(1).x, ray.GetPoint(1).y);
gunRot = Mathf.Atan2((mousePos.y + Player.transform.position.y * 1f), (mousePos.x + Player.transform.position.x * 1f));
I had the same problem as before. Why is this, and is it possible to get the ray to work correctly?
Comment