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 Blazza · Oct 26, 2011 at 01:50 PM · camerarotationthird-personcamera-look

Turning a character to match camera rotation

Hi all, apologies if this is a stupid question. I'm working on a 3rd person platformer, and I have the camera parented to an empty GameObject that is parented to the character. This allows me to move the camera backwards and forwards without effecting the rotation, which is done via mouse (the mouselook script is attached to the GameObject, rather than the camera). What I'd like to do, is to have the character stand still when the camera rotates around him, but then turn to face away from the camera as soon as any movement button is pressed. I thought I had it working well, but then I realised that if I rotate the camera around him several times, when I start moving he'll do those extra spins rather than just turning the difference. I tried all sorts of things like dividing the camera's rotation by 360 to get the number of spins etc. but I just feel like I'm on the wrong track there and can't quite get the right result. I'm sure I'm missing a very simple bit of code that I just can't seem to find.

Any help would be much appreciated.

 cameraYRotation = cameraObject.GetComponent(Mouselook).currentYRotation; 
 playerRotation = Mathf.SmoothDamp(playerRotation,playerTargetRotation,playerRotationV,moveSmoothDamp);
                         
 if (Input.GetAxis("Vertical") + Input.GetAxis("Horizontal") != 0)
 playerTargetRotation = cameraYRotation;
 
 transform.rotation = Quaternion.Euler(0, playerRotation, 0);

If I can't manage to peg this down, I'll probably just add in this:

 if (cameraYRotation - playerRotation > 30)
     playerRotation = cameraYRotation - 30;
 if (playerRotation - cameraYRotation > 30)
     playerRotation = cameraYRotation + 30;

Which just means he'll turn with the camera 30 degrees behind.

Comment
Add comment · Show 2
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 syclamoth · Oct 26, 2011 at 02:32 PM 0
Share

If I were you, I wouldn't make the camera an explicit transform child of the character. If you want to avoid the camera rotating when the character turns, make the camera centre object have no transform parent, but have a script on it which sets its position to that of your character every Update.

avatar image Blazza · Oct 27, 2011 at 03:46 PM 0
Share

Thanks for that syclamoth, I probably should have said 3rd person shooter rather than platformer. Anyway, I ended up adding that last bit (except 90 ins$$anonymous$$d of 30) and it seems to work pretty well after a couple of tweaks. But don't worry, I'm sure I'll have more questions soon!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

3rd Person Camera Geometry Navigation 1 Answer

Quaternion.LookRotation is making my object rotate around a point? 1 Answer

3rd Person Camera with fixed rotation 1 Answer

A little help with this camera script please? 3 Answers

3rd person camera view lock 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