Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JokerSmouker · Jan 24, 2017 at 11:19 AM · strange ioc

Help with managing your character with the camera

When the game character model starts to twitch when driving. Through trial and error, it turned out that the case in camera script. How to fix this problem ?? Help!!!

[System.Serializable]

         public class CameraController : MonoBehaviour
         {
             // =================================    
             // Nested classes and structures.
             // =================================

             // ...

             public struct ClipPlanePoints
             {
                 public Vector3 UpperLeft;
                 public Vector3 UpperRight;
                 public Vector3 LowerLeft;
                 public Vector3 LowerRight;
             }

             // =================================    
             // Variables.
             // =================================

             // ...

             new Camera camera;

             // Transforms.

             [Header("Transforms")]

             public Transform target;
             public Transform offset;

             // Position.

             [Header("Position")]

             public float followTargetSpeed = 8.0f;

             // Rotation.

             [Header("Rotation")]

             Vector2 mouseInput;

             public float mouseRotateAmount = 5.0f;
             public float mouseRotateSpeed = 5.0f;

             public float rotateZSpeed = 1.0f;
             public float resetZRotationSpeed = 5.0f;

             public Vector2 lookUpDownRange = new Vector2(-80.0f, 80.0f);

             // Zoom.

             [Header("Zoom")]

             public float zoomDistance = 4.0f;

             public float mouseZoomAmount = 1.0f;
             public float mouseZoomSpeed = 5.0f;

             public float mouseZoomMin = 1.0f;
             public float mouseZoomMax = 5.0f;

             // ...

             [Header("Occlusion Zoom")]

             public float occlusionZoomSpeed = 8.0f;
             public float occlusionZoomOffset = 0.0f;

             public float occlusionRaycastDownDistance = 0.5f;

             public AnimationCurve occlisionRaycastDownCurve =
                 new AnimationCurve(new Keyframe(0.0f, 1.0f), new Keyframe(1.0f, 0.0f));

             // Offset framing.

             [Header("Offset (Framing)")]

             public float offsetFramingAmount = 2.0f;
             public float offsetFramingSpeed = 1.0f;

             public float minPlayerSpeedForOffsetFraming = 1.0f;
             public float minMouseMoveForOffsetFraming = 1.0f;

             Vector3 offsetStartPosition;

             Vector3 offsetPositionTarget;
             Vector3 offsetMouseFramePosition;

             // Stored locally as euler because Quaternions take shortest
             // path around for rotation. By using euler, I don't run into 
             // the problem of spinning the camera too quickly and causing 
             // a sudden snap because the rotation exceeded 180 degrees.

             Vector3 rotationEuler;
             Vector3 targetRotationEuler;

             // Player.

             Player player;

             // =================================    
             // Functions.
             // =================================

             // ...

             void Awake()
             {

             }

             // ...

             void Start()
             {
                 lockMouse();

                 camera = Camera.main;
                 player = FindObjectOfType<Player>();

                 zoomDistance = Vector3.Distance(camera.transform.position, target.position);

                 offsetStartPosition = offset.localPosition;
                 offsetPositionTarget = offsetStartPosition;
             }

             // ...

             void lockMouse()
             {
                 Cursor.visible = false;
                 Cursor.lockState = CursorLockMode.Locked;
             }
             void unlockMouse()
             {
                 Cursor.visible = true;
                 Cursor.lockState = CursorLockMode.None;
             }

             // ...

             void FixedUpdate()
             {

             }

             // ...

             void Update()
             {
                 // Input.

                 mouseInput.x = Input.GetAxis("Mouse X");
                 mouseInput.y = Input.GetAxis("Mouse Y");

                 if (Input.GetMouseButtonDown(0))
                 {
                     lockMouse();
                 }
                 else if (Input.GetKeyDown(KeyCode.Escape))
                 {
                     unlockMouse();
                 }
             }

             // ...

             void LateUpdate()
             {                    
                 // Follow position.

                 transform.position = Damping.dampVector3(transform.position, target.position, followTargetSpeed, Time.deltaTime);

                 if (Cursor.lockState == CursorLockMode.Locked)
                 {
                     // Rotation.
                     // Can't get it to smooth when rotating, so I just set the physica time step to 0.01f (half of default).

                     targetRotationEuler.y += mouseInput.x * mouseRotateAmount;
                     targetRotationEuler.x += -mouseInput.y * mouseRotateAmount;

                     targetRotationEuler.x = Mathf.Clamp(targetRotationEuler.x, lookUpDownRange.x, lookUpDownRange.y);

                     targetRotationEuler.z += mouseInput.x * rotateZSpeed;

                     rotationEuler = Damping.dampVector3(rotationEuler, targetRotationEuler, mouseRotateSpeed, Time.deltaTime);

                     transform.eulerAngles = rotationEuler;

                     //transform.rotation *= Quaternion.AngleAxis(mouseRotateSpeed * Time.deltaTime, Vector3.up);

                     // Offset framing.

                     float playerSpeed = player.rb.velocity.magnitude;

                     if (playerSpeed < minPlayerSpeedForOffsetFraming)
                     {
                         offsetMouseFramePosition = offsetStartPosition;
                     }
                     else
                     {
                         if (mouseInput.x > minMouseMoveForOffsetFraming)
                         {
                             offsetMouseFramePosition.x = -offsetFramingAmount;
                         }
                         else if (mouseInput.x < -minMouseMoveForOffsetFraming)
                         {
                             offsetMouseFramePosition.x = offsetFramingAmount;
                         }

                         if (mouseInput.y > minMouseMoveForOffsetFraming)
                         {
                             offsetMouseFramePosition.y = -offsetFramingAmount;
                         }
                         else if (mouseInput.y < -minMouseMoveForOffsetFraming)
                         {
                             offsetMouseFramePosition.y = offsetFramingAmount;
                         }
                     }

                     offsetPositionTarget = Damping.dampVector3(offsetPositionTarget, offsetMouseFramePosition, offsetFramingSpeed, Time.deltaTime);

                     // Balance out rotation around Z back to 0.0f.

                     targetRotationEuler.z = Damping.dampFloat(targetRotationEuler.z, 0.0f, resetZRotationSpeed, Time.deltaTime);

                     // Zoom.

                     // Scrolling happens as 0.1f increments. x10.0f to make it 1.0f.

                     float scroll = Input.GetAxis("Mouse ScrollWheel") * 10.0f;

                     zoomDistance -= scroll * mouseZoomAmount;
                     zoomDistance = Mathf.Clamp(zoomDistance, mouseZoomMin, mouseZoomMax);

                     // Player occlusion check and zoom.

                     RaycastHit hitInfo;

                     checkIfPlayerOccluded(out hitInfo);
                     //float nearestDistance = checkCameraPoints(out hitInfo);

                     Vector3 finalOffsetPositionTarget;

                     RaycastHit hitInfo2;

                     Transform cameraTransform = camera.transform;

                     Vector3 directionToTarget = (target.position - cameraTransform.position).normalized;
                     Vector3 end = target.position - (directionToTarget * (zoomDistance + occlusionZoomOffset));

                     Physics.Raycast(end, Vector3.down, out hitInfo2, occlusionRaycastDownDistance);
                     Debug.DrawLine(end, end + (Vector3.down * occlusionRaycastDownDistance), Color.white);

                     if (hitInfo.collider)
                     {
                         //finalOffsetPositionTarget = offsetPositionTarget.normalized * nearestDistance;
                         finalOffsetPositionTarget = offsetPositionTarget.normalized * hitInfo.distance;
                     }
                     //if (hitInfo2.collider)
                     //{                            
                     //    float curveResult = 1.0f - occlisionRaycastDownCurve.Evaluate(hitInfo2.distance / occlusionRaycastDownDistance);
                     //    finalOffsetPositionTarget = (offsetPositionTarget.normalized * zoomDistance) * curveResult;
                     //}
                     //else if (hitInfo.collider)
                     //{
                     //    finalOffsetPositionTarget = Vector3.zero;
                     //}
                     else
                     {
                         finalOffsetPositionTarget = offsetPositionTarget.normalized * zoomDistance;
                     }

                     offset.localPosition = Damping.dampVector3(offset.localPosition, finalOffsetPositionTarget, mouseZoomSpeed, Time.deltaTime);
                 }
             }

             // ...

             void checkIfPlayerOccluded(out RaycastHit hitInfo)
             {
                 if (!camera)
                 {
                     camera = Camera.main;
                 }

                 Transform cameraTransform = camera.transform;

                 Vector3 directionToTarget = (target.position - cameraTransform.position).normalized;

                 Vector3 start = target.position;
                 Vector3 end = Application.isPlaying ?

                     (target.position - (directionToTarget * (zoomDistance + occlusionZoomOffset))) :
                     (offset.position + (-directionToTarget * occlusionZoomOffset));

                 Physics.Linecast(start, end, out hitInfo);

                 if (!Application.isPlaying)
                 {
                     Gizmos.color =
                         hitInfo.collider ? Color.red : Color.green;

                     Gizmos.DrawLine(start, end);
                 }
                 else
                 {
                     Debug.DrawLine(start, end, hitInfo.collider ? Color.red : Color.green);
                 }
             }

             // ...

             void OnDrawGizmos()
             {
                 RaycastHit hitInfo;

                 checkIfPlayerOccluded(out hitInfo);
                 //checkCameraPoints(out hitInfo);
             }

             // =================================    
             // End functions.
             // =================================

         }

         // =================================    
         // End namespace.
         // =================================

     }

 }

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Avoiding Bloom Pop 1 Answer

Enabling disabling objects via raycast 0 Answers

Problem in loading death menu scene 1 Answer

Letting the player place objcts using navmesh 0 Answers

How to make a script slow down to a stop 0 Answers


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