- Home /
Camera Script Amalgamation == Camera Script Abomination, Im In Over My Head
This is the camera script that Im currently using & trying to refine. I merged a smooth follow camera js with a mouse orbit js & removed anything that "broke" it. I added a mouse lock & an "aim" trigger. Instantly upon looking this you will notice that I really do not know what I'm doing. I've been at this Unity thing for about 4 weeks and am trying to learn as quickly as possible, but trying to control the camera is frustrating me to no end and I need some help. I left in all the authors comments and added my own where I added and/or changed things(MH). Please, have a look and leave any helpful comments you can. Thank you for your time. >
//MH This script is to be attached to main camera
//MH This entire script is a "smooth follow" camera.js and an "orbit mouse" camera.js merged together.
//MH this script allows the player to be followed by the camera in free form manner: reverting to "behind the player" only when chasing up
//MH you can "orbit" around the player at will but when moving mouse left/right while moving player via WASD causes player direction to follow mouse---I MUST FIX THIS
//MH Holding down "fire2" to aim causes a flicker effect(maybe the camera is bouncing between both original position & aim position due to a coding conflict?)
//MH There is no "smoothness" between aim and original position - the cameras instantly switch between each other---I MUST FIX THIS
//MH Out of fear of breaking this script further Ive left in all variables from both scripts---I MUST FIX THIS
enum Axes {MouseXandY, MouseX, MouseY} var Axis : Axes = Axes.MouseXandY;
 
               var sensitivityX = 15.0; var sensitivityY = 15.0;
 var minimumX = -360.0; var maximumX = 360.0;
 var minimumY = -60.0; var maximumY = 60.0;
 var rotationX = 0.0; var rotationY = 0.0;
 var lookSpeed = 2.0;
 var cameraReference : Camera;
  // The target we are following
 var target : Transform;
 // The distance in the x-z plane to the target
 var distance = 3;
 // the height we want the camera to be above the target
 var height = 0.5; var heightDamping = 2.0; var rotationDamping = 3.0;
 function Update () { //MH this locks the cursor in the middle of the screen & hides it
   Screen.lockCursor = true;
 //MH reveals & releases the cursor
 if (Input.GetKeyDown ("escape")) Screen.lockCursor = false;
   if (Axis == Axes.MouseXandY)
  {
     // Read the mouse input axis
     rotationX += Input.GetAxis("Mouse X") * sensitivityX;
     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  // Call our Adjust to 360 degrees and clamp function
      Adjust360andClamp();
 // Most likely you wouldn't do this here unless you're controlling an object's rotation.
 // Call our look left and right function.
      KeyLookAround();
  // Call our look up and down function.
      KeyLookUp();
 }
 else if (Axis == Axes.MouseX)
 {
  // Read the mouse input axis
      rotationX += Input.GetAxis("Mouse X") * sensitivityX;
  // Call our Adjust to 360 degrees and clamp function
      Adjust360andClamp();
 // if you're doing a standard X on object Y on camera control, you'll probably want to
 // delete the key control in MouseX. Also, take the transform out of the if statement.
 // Call our look left and right function.
      KeyLookAround();
 // Call our look up and down function.
      KeyLookUp();
 }
 else
 {
  // Read the mouse input axis
      rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
  // Call our Adjust to 360 degrees and clamp function
      Adjust360andClamp();
  // Call our look left and right function.
      KeyLookAround();
  // Call our look up and down function.
      KeyLookUp();
 }
 }
 function KeyLookAround () { //If you're not using it, you can delete this whole function.
 //Just be sure to delete where it's called in Update.
  // Call our Adjust to 360 degrees and clamp function
 Adjust360andClamp();
 // Transform our X angle
 transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
 }
 function KeyLookUp () { // Adjust for 360 degrees and clamp
  Adjust360andClamp();
 // Transform our Y angle, multiply so we don't loose our X transform
 transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
 }
 function Adjust360andClamp () { // This prevents your rotation angle from going beyond 360 degrees and also
 // clamps the angle to the min and max values set in the Inspector.
  // During in-editor play, the Inspector won't show your angle properly due to
 // dealing with floating points.
 //Uncomment this Debug line to see the angle in the console.
  // Debug.Log (rotationX);
 // Don't let our X go beyond 360 degrees + or -
 if (rotationX < -360)
 {
     rotationX += 360;
 }
 else if (rotationX > 360)
 {
     rotationX -= 360;
 }   
 // Don't let our Y go beyond 360 degrees + or -
 if (rotationY < -360)
 {
     rotationY += 360;
 }
 else if (rotationY > 360)
 {
     rotationY -= 360;
 }
 // Clamp our angles to the min and max set in the Inspector
 rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
 }
 function Start () { // Make the rigid body not change rotation
  if (rigidbody)
 {
     rigidbody.freezeRotation = true;
 }
 }
 function LateUpdate () {
  // Early out if we don't have a target
 //MH I nixed the GetButtonDown so I can have a constantly repeating button
 if (Input.GetButton ("Fire2")) {
 //MH I want the player to be able to aim all over the screen so, we:
 //MH tell the code to find player01 object
 //MH I got "moveDirection" from the ThirdPersonController.js which controls player01
 //MH transform the camera view to behind player01 & rotate with it
 //MH This chunk of code kind of works but seems to cause "flicker" - FIND A BETTER WAY!
  player01 = GameObject.Find("player01"); moveDirection = transform.TransformDirection(Vector3.forward); player01.transform.rotation = Quaternion.LookRotation(moveDirection);
 //MH this code draws a line from player01 to wherever player01 is aiming
  var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100)) { // Get your player object's position
  var player01T = GameObject.Find("player01").transform;
 // Now you have where your player is and a 3D point the mouse is over
  Debug.DrawLine(player01T.position, hit.point); } }
 if (!target) return;
 // Calculate the current rotation angles
 wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height;
 currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y;
 // Convert the angle into a rotation
 // The quaternion interface uses radians not degrees //so we need to convert from degrees to radians
 currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
 // Damp the rotation around the y-axis
 currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
 currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
 // Set the position of the camera on the x-z plane to:
 // distance meters behind the target
 transform.position = target.position; transform.position -= currentRotation  Vector3.forward  distance;
 // Set the height of the camera
 transform.position.y = currentHeight;
 }  
Your answer
 
 
             Follow this Question
Related Questions
Jittery Movement - specific mechanics in mind 0 Answers
Regarding transform.position in the roll a ball tutorial 1 Answer
rotate Y axis to face mouse 1 Answer
Camera Movement 0 Answers
Set Max Rotation On Weapon Sway 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                