Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var transformList:Transform[];
- var mousePlane:GameObject;
- var cam:Camera;
- var planeOffset:float = -1f;
- var mouseLocationIndicator:Transform;
- function Start() {
- if (!cam) {
- if (!Camera.main) {
- Debug.LogError("No Main Camera found. Please either assign a camera, or tag an existing camera as 'Main Camera'.");
- Destroy(this); //Remove the script since it will only cause errors if it continues to run with no camera assigned.
- return;
- }
- cam = Camera.main;
- }
- //host the mousePlane to the camera's transform, so they move and rotate together
- mousePlane.transform.parent = cam.transform;
- //Hide the plane so it doesn't show up in the scene.
- mousePlane.renderer.enabled = false;
- //offset the plane some distance from the camera to make the objects (eg, eyes) turn more or less to look at the mouse.
- if (planeOffset<0f) {planeOffset=cam.nearClipPlane*2f;}
- mousePlane.transform.position = cam.transform.position + cam.transform.forward * planeOffset;
- //rotate the plane so its normal points directly towards the camera (so rays cast from the camera will intersect with it)
- mousePlane.transform.up = -cam.transform.forward;
- }
- function Update () {
- //The point underneath the mouse on the screen covers a line in 3D. So we use a Ray
- //(a kind of straight line) intersected with a plane to find the point we want the
- //object(s) to look at.
- var ray:Ray = cam.ScreenPointToRay(Input.mousePosition);
- //Find the point of intersection between the ray and the plane.
- var hitinfo:RaycastHit;
- if (Physics.Raycast(ray,hitinfo)) {
- //If there is a point of intersection, make all the objects look at it...
- for (var i:int=0; i<transformList.Length; i++) {
- transformList[i].LookAt(hitinfo.point);
- }
- //... and optionally place the mouseLocationIndicator at the point of intersection.
- if (mouseLocationIndicator){
- mouseLocationIndicator.position = hitinfo.point;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment