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 BoscoStudios · Apr 06, 2020 at 04:49 AM · camerarotationrotatecamera rotateparent-child

Reset player movement axes after camera rotation

I have a character controller and camera setup so that the camera moves with the player in a 3D top down configuration. I've scripted the ability to rotate the camera by holding shift and moving the mouse. Once the camera has been rotated and shift released, I want the axes of the player movement to reset to the new camera rotation so pressing "w" still causes the player to travel towards the top of the screen. My idea for the solution was to create a parent "playerAxes" for the player gameobject which rotates to the camera target when shift is released. However, my (attempted) implementation of this causes the player to go flying off the playing field.

cameraController:

 public float RotationSpeed;
     public Transform Target, Player, PlayerAxes;
     float mouseX;
     Quaternion lastTargetRotation;
     
     void Start () {
         transform.LookAt (Target);
     
     }
     
     void LateUpdate () {
 
         CamControl();
     
     }
 
     void CamControl()
     {
         //Hold shift to move camera around player
         if(Input.GetKey ("left shift")){
         mouseX += Input.GetAxis ("Mouse X") * RotationSpeed;
         transform.LookAt (Target);
 
         Target.rotation = Quaternion.Euler (0, mouseX, 0);
         }
 
         if(Input.GetKeyUp ("left shift")){
             PlayerAxes.rotation = transform.rotation;
         }
 
 }

CharacterController:

 void PlayerMovement() {
             
         float hor = Input.GetAxis ("Horizontal");
         float ver = Input.GetAxis ("Vertical");
         Vector3 playerMovement = new Vector3(hor, 0f, ver).normalized * speed * Time.deltaTime;
         transform.Translate (playerMovement, Space.World);
         if(playerMovement != new Vector3 (0f, 0f, 0f)){
         transform.rotation = Quaternion.LookRotation (playerMovement);
         }
 
 
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Apr 06, 2020 at 09:53 PM

Don't use a parent object here, since if you mess with the rotation of the parent object it will cause your player to change position (rotating the parent object will cause the player to orbit around the center of the parent object). Instead just keep track of the rotation about the y axis and then rotate your movement vector by that amount.

 //In your camera script
 public static float playerMovementAngle;
 
 void Update () {
     //...
     if (Input.GetKeyUp ("left shift")) {
         playerMovementAngle = mouseX;
     }
 }
 
 //In your movement script
 void PlayerMovement () {
 
     float hor = Input.GetAxis ("Horizontal");
     float ver = Input.GetAxis ("Vertical");
     Vector3 playerMovement = new Vector3 (hor, 0f, ver).normalized * speed * Time.deltaTime;
 
     //Rotate your movement vector by your camera rotation.
     playerMovement = Quaternion.Euler (0, RotationSpeed.playerMovementAngle, 0) * playerMovement;
 
     transform.Translate (playerMovement, Space.World);
     if (playerMovement != new Vector3 (0f, 0f, 0f)) {
         transform.rotation = Quaternion.LookRotation (playerMovement);
     }
 
 }
Comment
Add comment · Show 1 · Share
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 BoscoStudios · Apr 07, 2020 at 04:09 AM 0
Share

This worked, thanks!

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

215 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

Related Questions

Top down camera for object moving on sphere 1 Answer

How to make 90 degree snapping camera, center on object? 0 Answers

Rotate Character Controller Camera 0 Answers

fixed rotation around object 1 Answer

How to Rotate FPS Camera independent of Parent Player Object while WallRunning ? 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