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 /
  • Help Room /
avatar image
0
Question by Jankaro · Jul 16, 2020 at 04:10 PM · camerarotationplayerquaternionorientation

Maintain fixed vision while the player turn

Hi, I have spent a week researching how I can achieve this but I didn't.

Basically, I have a player divided into three objects: the body, the head as child of the body and the camera as child of the head. The camera as usually only rotates on the x axis (up and down), the head on the other hand only rotates on the y axis (left and right). With this I get a regular view for a 3D player.

In the game you can jump from platform to platform, but these platforms can be rotated at any angle and when the player collides with them, the body is reoriented to be aligned on the y axis of these platforms. Something as if those platforms were the new ground.

My problem here is that I wanted to keep the player's vision fixed at the same point it was before the jump, even though the body is rotating towards the new orientation. This obviously does not happen since by rotating the body the head remains in its same local rotation. I just tried not to rotate the head, the problem here was that by turning the body, and not the head, the head rotates from its old orientation.

I leave here the script that controls the rotation of the player:

 public class RotationRB
 {
     public bool lockRot;
 
     private Transform player;
     private Transform head;
     private Transform myCam;
 
     private float maxCameraRot, rotSpeed;
     private int rotatingCount;
 
     private Vector3 camRot;
 
     public RotationRB(Transform player, Transform head, Transform cam, float maxCamRot, float rotSpeed)
     {
         this.player = player;
         this.head = head;
         myCam = cam;
         maxCameraRot = maxCamRot;
         this.rotSpeed = rotSpeed;
     }
 
     public void Rotate(float h, float v)
     {
         #region Rotacion
         if (!lockRot)
         {
             head.Rotate(0, h, 0);
 
             camRot = myCam.localEulerAngles + new Vector3(-v, 0, 0);
             camRot.x = ClampAngle(camRot.x, -maxCameraRot, maxCameraRot);
             myCam.localEulerAngles = camRot;
         }
         #endregion
     }
 
     float ClampAngle(float angle, float min, float max)
     {
         if (angle < 0f) { angle = 360 + angle; }
         if (angle > 180f) { return Mathf.Max(angle, 360 + min); }
         return Mathf.Min(angle, max);
     }
 
     #region Public Methods
 //This method reset player rotation when touching ground before a platform
     public void ResetPlayer()
     {
         PlayerRB.player.RotateTo(player, Quaternion.identity);
     }
 
     public Vector3 LookingDirection()
     {
         return myCam.TransformDirection(Vector3.forward);
     }
 
 //This coroutine rotates an obj to a new orientation and is called from player manager (PlayerRB.player.RotateTo())
     public IEnumerator RotationCoroutine(Transform obj, Quaternion rot)
     {
         lockRot = true;
         rotatingCount++;
         
         while (obj.rotation != rot)
         {
             obj.rotation = Quaternion.RotateTowards(obj.rotation, rot, rotSpeed * Time.fixedDeltaTime);
             yield return new WaitForFixedUpdate();
         }
         
         rotatingCount--;
         if(rotatingCount == 0) lockRot = false;
     }
 
     public IEnumerator RotationCoroutine(Transform obj, Transform unrotObj, Quaternion rot)
     {
         lockRot = true;
         rotatingCount++;
         Quaternion unRot = unrotObj.rotation;
         
         while (obj.rotation != rot)
         {
             obj.rotation = Quaternion.RotateTowards(obj.rotation, rot, rotSpeed * Time.fixedDeltaTime);
             unrotObj.rotation = unRot;
             yield return new WaitForFixedUpdate();
         }
         
         rotatingCount--;
         if (rotatingCount == 0) lockRot = false;
     }
     #endregion

First of all thank you very much for reading and I hope that I have managed to explain. Let me know if you need any extra information or I have not explained something properly. UnityAnswers also doesn't allow me to upload a couple of gifs that I have to illustrate the explanation.

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

318 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 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 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

Help with rotating camera around object based on mouse input 0 Answers

Camera Rotation Around Player 1 Answer

Rotating Minimap - Boundary Icons don't rotate 0 Answers

Add Rotation from parent to Quaternion Child 1 Answer

Camera not rotating alongside player character after new script. 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