- Home /
object follow mouse position in 3d world.
here code that i using
var s : Transform; function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
//Debug.Log(hit.point);
var a = hit.point;
s.position = a;
}
}
it not working. something like object go to camera position. how to fix it?
my camera position like 2d camera.. but i want to make 3d world
Answer by TomHunt · Apr 09, 2011 at 11:55 AM
if you want your world to look 3D, you should be using a perspective camera. in that case, you will want to use Camera.ScreenToWorldPoint(), which takes view space coordinates. do this for both zNear and zFar because the x/y values of each point will almost always be different in perspective view. ray.origin is the transformed zNear point, and ray.direction is the normalized delta between then zFar and zNear point.
actually you do not need to normalize the direction value. you can just use var m = Input.mousePosition; var nearPt = Vector3(m.x,m.y,Camera.nearClipPlane); var farPt = Vector3(m.x,m.y,Camera.farClipPlane); ray.origin = ScreenToWorldPoint(zNear); ray.direction = ScreenToWorldPoint(farPt)-ray.origin; ...
Your answer
Follow this Question
Related Questions
Make Object Move to/follow Another Object? (plus turn towards it) 3 Answers
How to make 3D object become the cursor? 2 Answers
Rotation Object Follow Help 2 Answers
Player not following touch after camera is rotated? 0 Answers
Generate unlimited map 1 Answer