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 MisterFox · Feb 03, 2018 at 01:39 AM · camerarotationrelative rotation

Rotating Player relative to Camera Rotation

Hey there, I'm sure this is a simple one but I cannot wrap my head around it. Basically, I have a 3D 3rd Person game, and these objects:

  1. Player that can move on X and Z,

  2. Camera Rig that follows player and can be rotated on Y

  3. Main Camera attached to Camera Rig

I've managed to have the player move using the horizontal and vertical input-axis relative to the camera rotation, but now I want him to rotate accordingly as well. I'm just not sure what values to mix to get it done - ideally, I would want the player to not just jump into the correct rotation, but actually have him gradually rotate there. I appreciate any solutions, ideas and nods into the right direction, pun not intended. Thanks in advance!

Here be the code for the Player:

 Rigidbody rb;

 float hAxis; //Input Horizontal Axis
 float vAxis; //Input Vertical Axis

 public GameObject camRig;
 Transform camRot; //Y-rotation of camera

 public float moveSpeed;

 void Start () {
     rb = GetComponent<Rigidbody> ();
     camRot = camRig.transform;
 }
 
 void Update () {
     hAxis = Input.GetAxis ("Horizontal");
     vAxis = Input.GetAxis ("Vertical");

     transform.Translate (
         camRot.forward * vAxis * moveSpeed * Time.deltaTime+
         camRot.right*hAxis*moveSpeed*Time.deltaTime, 
         Space.World);

     // Rotating when moving
     if (hAxis != 0 || vAxis != 0){

         //Code for rotating the player when he is in movement goes here

     }

 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IronKnigh · May 17, 2018 at 10:01 AM

Hey there! I would check out this tutorial to get your rotation working, since it goes hand in hand with what you're trying to accomplish: https://www.youtube.com/watch?v=_DmUL9KK9Yk

Hope this helps!

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 IronKnigh · Aug 20, 2018 at 03:55 AM 0
Share

Ok so I've solved this very same issue before but it was a post of my own. I included my code and explained how to solve it there, feel free to check it out:

https://answers.unity.com/questions/1506873/how-to-animate-player-rotation-with-respect-to-cam.html?childToView=1544087#answer-1544087

avatar image
0

Answer by eidursveinn · May 17, 2018 at 10:25 AM

I'd recommend using a tweening engine such as DOTween which makes it easy to set up simple animations through code. Using DOTween you'd be able to call .DoRotate(angle, duration)on the object you want to rotate, having it animate smoothly instead of jumping.

Comment
Add comment · 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

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

141 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

Related Questions

How to rotate an object around itself relative to the camera using Slerp? 1 Answer

How to smoothly rotate camera around an object with touch 1 Answer

How do I stop my camera flying? 0 Answers

Why is my camera not snapping 90 degrees? 0 Answers

How to force camera rotation? 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