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 /
  • Help Room /
avatar image
0
Question by JsephRly · Oct 30, 2017 at 08:26 AM · quaternioncamera-movementcamera-lookcamera rotation

Making a fixed camera system and having trouble with gimbal lock

I'm trying to make a fixed camera system reminiscent to MGS2's. So far I've got panning on induvidual axises working fine, but I'm struggling with rotation. I've managed to get induvidual axises of the camera to rotate relative to the player, but they'll only do this whithin certain intervals, so once the player moves a certain distance (or reaches a certain coordinate) the rotation will jerk or reverse suddenly.

I know this is likely due to gimbal lock, but I don't know what quaternion functions to use in order to acheive an effect like this with full 360 degree rotation.

Also, as I'm new to coding, there's probably a way better more efficient way to do this (likely with far less Vector3s and if statements). I'm very aware that I may be doing this completely wrong so any criticism or general advice is appreciated.

      {


 public float smoothSpeed;

 //is the camera translating towards player
 public bool pan;
 public bool pitch;
 public bool roll;
 public bool yaw;


 public bool followX;
 public bool followY;
 public bool followZ;
 public bool lookX;
 public bool lookY;
 public bool lookZ;

 public bool justRotate;

 public Transform player;



 //defines camera translation

 Vector3 desiredPos;
 Vector3 panSmoothResult;

 private Vector3 panPosX;
 private Vector3 panPosY;
 private Vector3 panPosZ;

 private Vector3 panPosXY;
 private Vector3 panPosYZ;
 private Vector3 panPosXZ;
 private Vector3 panPosXYZ;


 public Vector3 defaultRotation;
 private Quaternion _defaultRotation;
 public Vector3 panOffset;
 public Vector3 lookOffset;
 public Vector3 wallHugOffset;
 public Vector3 firstPersonPosOffset;


 //the vars I'd like to use to control rotation 
 [SerializeField]
 Vector3 desiredRot;
 Quaternion _desiredRot;
 [SerializeField]
 Quaternion lookSmoothResult;


 Vector3 lookAxis;
 [SerializeField]
 Vector3 lookPosition;
 public float lookSpeed;

 //first person and wallhug vars
 public bool wallHug;
 Vector3 firstPersonPos;
 Vector3 firstPersonTransition;
 public bool firstPerson;
 public Transform playerHead;
 Vector3 wallHugPos;
 Vector3 wallHugRot;
 Vector3 wallHugTransition;


 void FixedUpdate () {

     //set vectors
     _defaultRotation.eulerAngles = defaultRotation;
     panPosX.Set(player.position.x, 0f, 0f);
     panPosY.Set(0f, player.position.y, 0f);
     panPosZ.Set(0f,0f, player.position.z);

     panPosXY = panPosX + panPosY;
     panPosXZ = panPosX + panPosZ;
     panPosYZ = panPosY + panPosZ;
     panPosXYZ = player.position;
     firstPersonPos = playerHead.position + firstPersonPosOffset;
     wallHugPos = player.position + wallHugOffset;
     lookPosition= player.position * lookSpeed;


     if(firstPerson){
         pan = false;
         pitch = false;
         roll = false;
         yaw = false;
         wallHug = false;
         FirstPersonPosition();
     }
     if(!firstPerson && !wallHug){
         pan = true;
         transform.rotation = _defaultRotation;
     }
     if(wallHug){
         pan = false;
         firstPerson = false;
         WallHugPosition();
     }
     if(!firstPerson && !wallHug && justRotate){
         pan = false;
     }

 
     if (pan) {
     
     if(followX && followY && followZ && pan){
             PanCamera (panPosXYZ);
     }
     else if(followX && followY && pan){
             PanCamera (panPosXY);
     }
     else if(followX && followZ && pan){ 
             PanCamera (panPosXZ);
     }
     else if(followY && followZ && pan){
             PanCamera (panPosYZ);
     }
     else if(followX && pan){
             PanCamera (panPosX);
     } 
     else if(followY && pan){
              PanCamera (panPosY);
     }
     else if (followZ && pan){
             PanCamera (panPosZ);
     }
     }

     **if(pitch || roll || yaw){
         if(lookX && pitch){
             lookAxis.Set (lookPosition.x, 0f, 0f);
             LookCamera (lookAxis);
         }
         else if(lookY && pitch){
             lookAxis.Set (lookPosition.y, 0f, 0f);
             LookCamera (lookAxis);
         }
         else if(lookZ && pitch){
             lookAxis.Set (lookPosition.z, 0f, 0f);
             LookCamera (lookAxis);
         }
         else if(lookX && roll){
             lookAxis.Set (0f, lookPosition.x, 0f);
             LookCamera (lookAxis);
         }
         else if(lookY && roll){
             lookAxis.Set (0f, lookPosition.y, 0f);
             LookCamera (lookAxis);
         }
         else if(lookZ && roll){
             lookAxis.Set (0f, lookPosition.z, 0f);
             LookCamera (lookAxis);
         }
         else if(lookX && yaw){
             lookAxis.Set (0f, 0f, lookPosition.x);
             LookCamera (lookAxis);
         }
         else if(lookY && yaw){
             lookAxis.Set (0f, 0f, lookPosition.y);
             LookCamera (lookAxis);
         }
         else if(lookZ && yaw){
             lookAxis.Set (0f, 0f, lookPosition.z);
             LookCamera (lookAxis);
         }
         else if(lookX && pitch && roll){
             lookAxis.Set (lookPosition.x,lookPosition.x,0f);
             LookCamera (lookAxis);
         }
         else if(lookY && pitch && roll){
             lookAxis.Set (lookPosition.y,lookPosition.y,0f);
             LookCamera (lookAxis);
         }
         else if(lookZ && pitch && roll){
             lookAxis.Set (lookPosition.z,lookPosition.z,0f);
             LookCamera (lookAxis);
         }
         else if(lookZ && pitch && roll){
             lookAxis.Set (lookPosition.z,lookPosition.z,0f);
             LookCamera (lookAxis);
         }
         else if(lookX && pitch && yaw){
             lookAxis.Set (lookPosition.x,0f, lookPosition.x);
             LookCamera (lookAxis);
         }
         else if(lookY && pitch && yaw){
             lookAxis.Set (lookPosition.y,0f, lookPosition.y);
             LookCamera (lookAxis);
         }
         else if(lookX && roll && yaw){
             lookAxis.Set (0f, lookPosition.x, lookPosition.x);
             LookCamera (lookAxis);
         }
         else if(lookY && roll && yaw){
             lookAxis.Set (0f, lookPosition.y, lookPosition.y);
             LookCamera (lookAxis);
         }
         else if(lookZ && roll && yaw){
             lookAxis.Set (0f, lookPosition.z, lookPosition.z);
             LookCamera (lookAxis);
         }**
         
     }



 }



 /// generic functions for panning
 public Vector3 PanOffset(Vector3 panPos)
 {
     desiredPos = panPos + panOffset;
     return desiredPos;
 
 }

 **public Quaternion LookOffset(Vector3 lookAxis){
     desiredRot = lookAxis + lookOffset; 
     _desiredRot.eulerAngles = desiredRot;
     return _desiredRot;
 }**

 public Vector3 PanSmooth (Vector3 desiredPosition)
 {
     panSmoothResult = Vector3.Lerp (transform.position, desiredPos, smoothSpeed);
     return panSmoothResult;

 }


 **public Quaternion LookSmooth (Quaternion _desiredRot){
     lookSmoothResult = Quaternion.Lerp(transform.rotation, _desiredRot, smoothSpeed);
     return lookSmoothResult;
 }**
 

 void PanCamera(Vector3 panAxis)
 {
     PanOffset (panAxis);
     PanSmooth (desiredPos);
     transform.position = panSmoothResult;
 }

 **void LookCamera(Vector3 lookAxis){
     LookOffset (lookAxis);
     LookSmooth (_desiredRot);
     transform.rotation = lookSmoothResult;
 }**


 void FirstPersonPosition()
 {    
     //would be nice to have this run once before 
     firstPersonTransition = Vector3.Lerp (transform.position, firstPersonPos, smoothSpeed);

     transform.position = firstPersonPos;
     transform.rotation = playerHead.rotation;
     transform.Rotate(0f, 0f, 90f);

 }
     
 void WallHugPosition(){
     wallHugTransition = Vector3.Lerp (transform.position, wallHugPos, smoothSpeed);
     transform.position = wallHugTransition;
     transform.LookAt (playerHead.position);
 }

}

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

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

Related Questions

My camera rotate but my player dont. How Ive to do so? 1 Answer

Top-Down Camera view (with rotation) need help (third person script) 0 Answers

how can i make my fps camera more smooth 0 Answers

,how to set max rotation for camera in third person game 0 Answers

Camera raycast replaces Mouse movement 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