- Home /
Arial camera get selected point
Hi all,
I have a camera which is like an Arial Camera. I am trying to select the point from mouse click but the behavior is not predictable. I am using following code to get the selected coordinates -
Vector3 targetpos;
float myPoint;
private Plane plane = new Plane(Vector3.up,0);
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
if (plane.Raycast (ray, out myPoint)) {
targetpos = ray.GetPoint (myPoint);
}
}
The problem is its not giving me the correct point. Is it the correct way to get the mouse clicked coordinate from arial camera? What changes should i do for getting correct solution , because this solution works correctly when applied for third person camera view.
I have also tried Physics.Raycast but both gives me a selection away from my mouse click region.
Little more explanation - I am trying to build a AI mover , now to move from its own location to other location it need the point (x,y,x) to travel to. This "travel to" point i am trying to select from a camera view (in this case camera is situated in the sky proving static view of the model) . Now with the above mentioned code i am trying to get my "travel to" point. It do give me point selected (x,y,z) but not at the exact spot where i selected it but elsewhere.
I'm not 100% sure of the problem - you are looking to get the coordinates on the 'plane' object you have, hence why you have plane.Raycast ?
Which particular point are you seeking to get?
Your question is confusing. First, I think you mean to use the word aerial not arial. $$anonymous$$ore importantly, you should define what point you expect to get. It would also help to know how the variables plane and myPoint are defined.
Do you want a point on the screen in screen coordinates (such as the one provided by Input.mousePosition) or do you want a point in world coordinates? If you want a point in world coordinates do you want the nearest point, a point a certain distance away from the camera, a point along the created ray that intersects with a plane, or a point along the created ray that intersects with a collider? All of these have different values and would use varying methods (Camera.ScreenToWorldPoint, Camera.ScreenPointToRay.GetPoint, Plane.Raycast, Physics.Raycast).
The code should work (assu$$anonymous$$g you are trying to get a point on the xz plane). However you wrote the solution works correctly when applied for the third person camera but not the aerial camera. Is the aerial camera always the only or the main camera?
If you are using a second camera then Camera.mainCamera.ScreenPointToRay (Input.mousePosition); returns the ray from the first camera tagged $$anonymous$$ainCamera, which might not be the aerial camera. To fix this, you would need to do something like
Vector3 targetpos;
float myPoint;
private Plane plane = new Plane(Vector3.up,0);
public Camera aerialCamera;
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
Ray ray = aerialCamera.ScreenPointToRay (Input.mousePosition);
if (plane.Raycast (ray, out myPoint)) {
targetpos = ray.GetPoint (myPoint);
}
}
assigning aerialCamera via script or in the inspector
If you always have one camera then how are the third person and aerial cameras different?
Answer by sparkzbarca · Apr 02, 2015 at 09:13 PM
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast (ray, out myPoint)) {
targetpos = mypoint.point;
}
}
notice
physics.raycast
and targetpos = mypoint.point
Answer by ishan00kaushik · Apr 02, 2015 at 10:38 PM
Thanks , worked like a charm . My mistake had two camera active.
Your answer
Follow this Question
Related Questions
Object Selection with large number of objects 1 Answer
Push object in direction of camera 0 Answers
Raycast Object Selection 3 Answers
Mouse plane does not detect height 1 Answer