Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 JsephRly · Oct 30, 2017 at 08:26 AM · cameracamera-movementcamera rotatecamera-look

Control camera rotation by induvidual axises through Vector3s (for a fixed camera system)

I'm trying to build a fixed camera system reminiscent of MGS2's. So far I've got camera translation working fine with public bools controlling induvidual axises.

I'd like to get rotation working in a similar way (for example, if the camera was static and "yaw" and "lookZ" were true, you'd get an angle similiar to going along the bridges in MGS2 where the camera turns towards the player's forward location), but I'm not sure about how to manipulate Quarternions with Vector3s to acheive this effect.

I've managed to get rotation roughly working for the wallHug and first person modes, but my camera will retain whatever rotation it had last when I go back to panning, so having a defaultRotation function/Quaternion would make the code a lot more functional.

I am quite new to coding so if there's any way to do this more efficiently or I'm just approaching this in the wrong way altogther, advice to that tune and other constructive criticism would also be greatly appreciated.

 public float smoothSpeed;

 public bool pan;
     //the bools I'd like to use to control rotation. Currently not functional
 public bool pitch;
 public bool roll;
 public bool yaw;


 public bool followX;
 public bool followY;
 public bool followZ;

     //these are also non functional. Would ideally work with pitch roll and yaw
  public bool lookX;
  public bool lookY;
  public bool lookZ;

 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 lookOffset;
 public Vector3 panOffset;
 public Vector3 wallHugOffset;
 public Vector3 firstPersonPosOffset;


 //the vars I'd like to use to control rotation 
 Vector3 desiredRot;
 Vector3 rotSmoothResult;

 Vector3 pitchX;
 Vector3 pitchY;
 Vector3 pitchZ;
 Vector3 pitchXY;
 Vector3 pitchXZ;
 Vector3 pitchYZ;

 Vector3 rollX;
 Vector3 rollY;
 Vector3 rollZ;
 Vector3 rollXY;
 Vector3 rollXZ;
 Vector3 rollYZ;


 Vector3 yawX;
 Vector3 yawY;
 Vector3 yawZ;
 Vector3 yawXY;
 Vector3 yawXZ;
 Vector3 yawYZ;

 //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
     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;
     
             /*would like to control the rotation like this through a CameraLook() function simmilar 
      to the CameraPan() group of functions. There's probably a better way to do it through a master 
            if statement*/
     pitchX.Set(player.position.x,0f,0f);
     pitchY.Set(player.position.y, 0f,0f);
     pitchZ.Set(player.position.z,0f,0f);
     rollX.Set(0f,player.position.x,0f);
     rollX.Set(0f,player.position.y,0f);
     rollX.Set(0f,player.position.z,0f);
     yawX.Set (0f,0f, player.position.x);
     yawX.Set (0f,0f, player.position.y);
     yawX.Set (0f,0f, player.position.z);

     if(firstPerson){
         pan = false;
         wallHug = false;
         FirstPersonPosition();
     }
     if(!firstPerson && !wallHug){
         pan = true;
         transform.rotation.SetLookRotation(lookOffset);
     }
     if(wallHug){
         pan = false;
         firstPerson = false;
         WallHugPosition();
     }
 
 
     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);
     }
     }

 }

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


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

 }
 // functions that control panning

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


 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

112 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

Related Questions

Rotate Camera to the object 0 Answers

camera movments fixed.. character controller without using character controller -.-' 2 Answers

How to have free rotation camera? 1 Answer

Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers

Unity - Dancing Ball World camera following and rotating system 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