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 Kabutak · Apr 16, 2014 at 09:00 PM · camerathird-person

3rd person camera reset bug

I'm working on writing up a camera control script for a game I am doing and have run into an odd issue. The camera itself controls fine, bound to the keyboard and a controller. However, upon hitting the reset button to center the camera it has some odd features. Normally it zooms out very slightly and 'pops' back in, which is somewhat jarring but not game breaking. The worst problem is when resetting from a high position, such as looking down at the player, in which it jumps much more noticeably. Also, upon migrating the script to C# in order to have more functionality it jumps terribly when resetting from below the terrain as well. Currently there is no collision with terrain or objects, that is my next step.

If anyone can see what is causing the jittery movements and jumping I would be most indebted. In the meantime I'm going to work on the collision, since I've hit a dead end on this particular issue.

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
     public Transform target;
     public float distance = 7.0F;
     private string state = "mid";
     
     public float height = 3.0F;
     public float heightDamping = 2.0F;
     public float rotationDamping = 3.0F;
     
     private float zoomtime = 0.0F;
     private float zoomdelay = 1.0F;
     private float camtime = 0.0F;
     private float camdelay = 0.5F;
     
     public float xSpeed = 180.0F;
     public float ySpeed = 80.0F;
     
     public float yMinLimit = -20.0F;
     public float yMaxLimit = 80.0F;
     
     private float x = 0.0F;
     private float y = 0.0F;
     
     private float wantedRotationAngle = 0.0F;
     private float wantedHeight = 0.0F;
         
     void Start () {
         Vector3 angles = transform.eulerAngles;
         x = angles.y;
         y = angles.x;
         
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
     
     void Update () {
         
         if (zoomtime <= zoomdelay)
         {
             zoomtime += Time.deltaTime;
         }
         
         if ((Input.GetAxisRaw("DPad_Vertical") == 1 || Input.GetAxisRaw("Mouse ScrollWheel") > 0)&& zoomtime >= zoomdelay)
         {
             if (state == "far")
             {
                 state = "mid";
                 distance = 7.0F;
             }
             else if (state == "mid")
             {
                 state = "close";
                 distance = 4.0F;
             }
             zoomtime = 0.0F;
         }
         else if((Input.GetAxisRaw("DPad_Vertical") == -1 || Input.GetAxisRaw("Mouse ScrollWheel") < 0) && zoomtime >= zoomdelay)
         {
             if (state == "close")
             {
                 state = "mid";
                 distance = 7.0F;
             }
             else if (state == "mid")
             {
                 state = "far";
                 distance = 10.0F;
             }
             zoomtime = 0.0F;
         }
         
         if (target && camtime >= camdelay/2) {
             x += Input.GetAxisRaw("Horizontal_R") * xSpeed * 0.02F;
             y -= Input.GetAxisRaw("Vertical_R") * ySpeed * 0.02F;
             
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
             Quaternion rotation = Quaternion.Euler(y, x, 0);
             Vector3 position = rotation * new Vector3(0.0F, 0.0F, -distance) + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
         }
         
         if(Input.GetButtonDown("ThumbstickButton_R"))
         {
             camtime = 0;
             wantedRotationAngle = target.eulerAngles.y;
             wantedHeight = target.position.y + height;
         }
         
         if (camtime <= camdelay)
         {
             camtime += Time.deltaTime;
             
             // Calculate the current rotation angles
             float currentRotationAngle = transform.eulerAngles.y;
             float currentHeight = transform.position.y;
             
             // Damp the rotation around the y-axis
             currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime *2);
             
             // Damp the height
             currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
             
             // Convert the angle into a rotation
             Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
             
             // 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
             Vector3 temp = transform.position;
             temp.y = currentHeight;
             transform.position = temp;
             
             // Always look at the target
             transform.LookAt (target);
             
             Vector3 angles = transform.eulerAngles;
             x = angles.y;
             y = angles.x;
         }
     }
     
     static float ClampAngle (float angle, float min, float max) {
         if (angle < -360)
             angle += 360;
         if (angle > 360)
             angle -= 360;
         return Mathf.Clamp (angle, min, max);
     }
 }
Comment
Add comment · Show 1
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
avatar image Kabutak · May 26, 2014 at 04:09 PM 0
Share

Update: Since then I have mostly fixed the collision and occlusion based problems. However the resetting camera continues to jump. I may have to simply remove that functionality if we cannot find a solution.

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

20 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 avatar image avatar image

Related Questions

3rd person camera that avoids objects 0 Answers

Need help for player rotation and camera rotation 0 Answers

Jittery Collision Detection 0 Answers

How to keep player moving in the same direction after camera change 1 Answer

Stop camera from going through walls 2 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