Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Exeneva · Jan 07, 2014 at 03:14 AM · camerashake

My Camera Shakes When My Character Runs (high speed)

This is my script, called thirdPersonCamera.cs

The idea is to always stay a set distance behind the player. The camera focuses on an empty GameObject at the center of the player character (this object is a child of the player character). It also zooms in when occluded and zooms back out when no longer occluded, but those parts don't have anything to do with the shakiness.

You can see it in action here: http://exeneva.com/UnityProjects/RogueFighter001

 using UnityEngine;
 using System.Collections;
 
 public class thirdPersonCamera : MonoBehaviour 
 {
     public thirdPersonCamera instance;                // Reference to the Main Camera.
     public Transform targetLookTransform;            // Transform of the camera will be looking at.
     public float distance = 5f;                        // Start distance of camera.
     public float distanceMin = 3f;                    // Minimum distance of camera zoom.
     public float distanceMax = 10f;                    // Maximum distance of camera zoom.
     public float distanceSmooth = 0.05f;            // Camera zooming smooth factor.
     public float distanceCameraResumeSmooth = 1f;    // Distance at which point smoothing is resumed after occlusion handling is no longer occuring.
     private float distanceCameraSmooth = 1f;        // Camera smoothing distance (after occlusion is no longer happening).
     private float preOccludedDistance = 0f;    
 
     public float mouseXSensitivity = 5f;            // Mouse X sensitivity.
     public float mouseYSensitivity = 5f;            // Mouse Y sensitivity.
     public float mouseWheelSensitivity = 5f;        // Mouse wheel/scroll sensitivity.
     public float xSmooth = 0.05f;                    // Smoothness factor for x position calculations.
     public float ySmooth = 0.1f;                    // Smoothness factor for y position calculations.
     public float yMinLimit = -40f;                    
     public float yMaxLimit = 80f;                    
     private float mouseX = 0f;
     private float mouseY = 0f;
     private float velocityX = 0f; 
     private float velocityY = 0f;
     private float velocityZ = 0f;
     private float velocityDistance = 0f;
 
     public float occlusionDistanceStep = 0.5f;
     public int maxOcclusionChecks = 10;                // Max number of times to check for occlusion.
 
     private float startDistance = 0f;
     private float desiredDistance = 0f;
 
     private Vector3 position = Vector3.zero;
     private Vector3 desiredPosition = Vector3.zero;
 
     private void Awake ()
     {
         instance = this;
     }
 
     void Start () 
     {
         distance = Mathf.Clamp (distance, distanceMin, distanceMax);    // Ensure our distance is between min and max (valid)
         startDistance = distance;
         Reset ();
     }
 
     private void LateUpdate () 
     {
         if (targetLookTransform == null)
         {
             return;
         }
 
         HandlePlayerInput ();
 
         int count = 0;
         do 
         {
             CalculateDesiredPosition ();
             count++;
         } while (CheckIfOccluded(count));
 
         UpdatePosition ();
     }
 
     private void HandlePlayerInput ()
     {
         float deadZone = 0.01f;
 
         // If right mouse button is down, get mouse axis input.
         if (Input.GetMouseButton (1)) 
         {
             mouseX += Input.GetAxis ("Mouse X") * mouseXSensitivity;
             mouseY -= Input.GetAxis ("Mouse Y") * mouseYSensitivity;
         }
 
         // Clamp (limit) mouse Y rotation. Uses thirdPersonCameraHelper.cs.
         mouseY = thirdPersonCameraHelper.clampingAngle (mouseY, yMinLimit, yMaxLimit);
 
         // Clamp (limit) mouse scroll wheel.
         if (Input.GetAxis ("Mouse ScrollWheel") > deadZone || Input.GetAxis ("Mouse ScrollWheel") < -deadZone)
         {
             desiredDistance = Mathf.Clamp (distance - Input.GetAxis("Mouse ScrollWheel") * mouseWheelSensitivity,
                                            distanceMin,
                                            distanceMax
                                            );
             preOccludedDistance = desiredDistance;
             distanceCameraSmooth = distanceSmooth;
         }
     }
 
     // Smoothing.
     private void CalculateDesiredPosition ()
     {
         // Evaluate distance.
         ResetDesiredDistance ();
         distance = Mathf.SmoothDamp (distance, desiredDistance, ref velocityDistance, distanceCameraSmooth);
 
         // Calculate desired position.
         desiredPosition = CalculatePosition (mouseY, mouseX, distance);
     }
 
     private bool CheckIfOccluded (int count)
     {
         bool isOccluded = false;
         float nearestDistance = CheckCameraPoints (targetLookTransform.position, desiredPosition);
 
         if (nearestDistance != -1)
         {
             if (count < maxOcclusionChecks)
             {
                 isOccluded = true;
                 distance -= occlusionDistanceStep;
 
                 // 0.25 is a good default value.
                 if (distance < 0.25f)
                 {
                     distance = 0.25f;
                 }
             }
             else
             {
                 distance = nearestDistance - Camera.main.nearClipPlane;
             }
             desiredDistance = distance;
             distanceCameraSmooth = distanceCameraResumeSmooth;
         }
 
         return isOccluded;
     }
 
     private Vector3 CalculatePosition (float rotX, float rotY, float rotDist)
     {
         Vector3 direction = new Vector3 (0, 0, -distance);             // -distance because we want it to point behind our character.
         Quaternion rotation = Quaternion.Euler (rotX, rotY, 0);
         return targetLookTransform.position + (rotation * direction);
     }
 
     private float CheckCameraPoints (Vector3 from, Vector3 to)
     {
         float nearestDistance = -1f;
 
         RaycastHit hitInfo;
 
         thirdPersonCameraHelper.ClipPlanePoints clipPlanePoints =
             thirdPersonCameraHelper.ClipPlaneAtNear (to);
 
         // Draw the raycasts going through the near clip plane vertexes.
         Debug.DrawLine (from, to + transform.forward * -camera.nearClipPlane, Color.red);
         Debug.DrawLine (from, clipPlanePoints.upperLeft, Color.red);
         Debug.DrawLine (from, clipPlanePoints.upperRight, Color.red);
         Debug.DrawLine (from, clipPlanePoints.lowerLeft, Color.red);
         Debug.DrawLine (from, clipPlanePoints.lowerRight, Color.red);
         Debug.DrawLine (clipPlanePoints.upperLeft, clipPlanePoints.upperRight, Color.red);
         Debug.DrawLine (clipPlanePoints.upperRight, clipPlanePoints.lowerRight, Color.red);
         Debug.DrawLine (clipPlanePoints.lowerRight, clipPlanePoints.lowerLeft, Color.red);
         Debug.DrawLine (clipPlanePoints.lowerLeft, clipPlanePoints.upperLeft, Color.red);
 
         if (Physics.Linecast(from, clipPlanePoints.upperLeft, out hitInfo) && hitInfo.collider.tag != "Player")
             nearestDistance = hitInfo.distance;
         if (Physics.Linecast(from, clipPlanePoints.lowerLeft, out hitInfo) && hitInfo.collider.tag != "Player")
             if (hitInfo.distance < nearestDistance || nearestDistance == -1)
                 nearestDistance = hitInfo.distance;
         if (Physics.Linecast(from, clipPlanePoints.upperRight, out hitInfo) && hitInfo.collider.tag != "Player")
             if (hitInfo.distance < nearestDistance || nearestDistance == -1)
                 nearestDistance = hitInfo.distance;
         if (Physics.Linecast(from, to + transform.forward * -camera.nearClipPlane, out hitInfo) && hitInfo.collider.tag != "Player")
             if (hitInfo.distance < nearestDistance || nearestDistance == -1)
                 nearestDistance = hitInfo.distance;
 
         return nearestDistance;
     }
 
     private void ResetDesiredDistance ()
     {
         if (desiredDistance < preOccludedDistance)
         {
             Vector3 pos = CalculatePosition (mouseY, mouseX, preOccludedDistance);
             float nearestDistance = CheckCameraPoints(targetLookTransform.position, pos);
 
             if (nearestDistance == -1 || nearestDistance > preOccludedDistance)
             {
                 desiredDistance = preOccludedDistance;
             }
         }
     }
 
     private void UpdatePosition ()
     {
         float posX = Mathf.SmoothDamp (position.x, desiredPosition.x, ref velocityX, xSmooth);
         float posY = Mathf.SmoothDamp (position.y, desiredPosition.y, ref velocityY, ySmooth);
         float posZ = Mathf.SmoothDamp (position.z, desiredPosition.z, ref velocityZ, xSmooth);
 
         position = new Vector3 (posX, posY, posZ);
 
         transform.position = position;
 
         transform.LookAt (targetLookTransform);
     }
 
     public void Reset ()
     {
         mouseX = 0f;
         mouseY = 10f;
         distance = startDistance;
         desiredDistance = distance;
         preOccludedDistance = distance;
     }
 
     public static void UseExistingOrCreateMainCamera () 
     {
         GameObject tempCamera;
         GameObject targetLookAt;
         thirdPersonCamera myCamera;
 
         if (Camera.main != null)
         {
             tempCamera = Camera.main.gameObject;
         }
         else
         {
             tempCamera = new GameObject("Main Camera");
             tempCamera.AddComponent("Camera");
             tempCamera.tag = "MainCamera";                // This is what actually makes our created camera the main camera.
         }
 
         tempCamera.AddComponent ("thirdPersonCamera");
         myCamera = tempCamera.GetComponent<thirdPersonCamera>();
 
         targetLookAt = GameObject.FindGameObjectWithTag ("CameraFocus");
 
         if (targetLookAt == null) 
         {
             targetLookAt = new GameObject ("Player");
             targetLookAt.transform.position = new Vector3(768f, 0, 900f);
         }
 
         myCamera.targetLookTransform = targetLookAt.transform;
     }
 }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

iTween Shake intensity issues 1 Answer

Camera shake scripts works but camera wont stay in prefab 2 Answers

camera follow problem-unity 4.3 2d 1 Answer

How can my window shake while im running? 1 Answer

Constant camera shake. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges