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 Mayday556 · Jul 30, 2013 at 10:27 PM · rotationrigidbodyeuleranglesfixedupdatey axis

My rigidbody's y rotation is always 180

So I have been messing around in Unity for quite sometime and decided to make a 3rd person game. I finished making the camera rotate around the player(although it may be a bit buggy), and I just started on the character movement and rotation. The movement seems to be working fine, but the rotation is being kind of buggy. I made the rigidbody's Y rotation equal to the camera Y rotation so the camera is always seeing his back. I tried this, thinking it would work and the rigidbody is always staying at 180 degrees on the Y-axis. I have been trying to debug it for hours on end and I haven't figured out why this is happening the the rigidbody. The code for the movement script is below:

using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

 public float playerSpeed = 7.0f;
 public float gravity = 10;
 
 private Vector3 moveDirection;
 private Transform myTransform;
 
 void Start() {
     myTransform = transform;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     
     Debug.Log(Camera.main.transform.eulerAngles.y);
     
     MovePlayer();
     RotatePlayer();
 }
 
 void MovePlayer() {
     moveDirection = new Vector3(playerSpeed * Input.GetAxis("Horizontal"), -gravity, playerSpeed * Input.GetAxis("Vertical"));
     
     //Transform the vector3 to local space
     moveDirection = transform.TransformDirection(moveDirection);
     
     //set the velocity, so you can move
     transform.rigidbody.velocity = new Vector3(moveDirection.x, transform.rigidbody.velocity.y, moveDirection.z);
     rigidbody.AddForce (Vector3.up * -10);
 }
 
 void RotatePlayer() {
     
     rigidbody.MoveRotation(new Quaternion(myTransform.rotation.x, Camera.main.transform.eulerAngles.y, myTransform.rotation.z, 0));
 }

}

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
0
Best Answer

Answer by robertbu · Jul 30, 2013 at 11:16 PM

Your main problem is on line 33. You are constructing a new Quaternion. The values x, y, z, w are not degrees, but instead are values between 0 and 1 and are non-intuitive. Unless you understand Quaternions, it not advised directly interact with the components. Unity provides a variety of helper functions. You could change your code to:

 rigidbody.MoveRotation(Quaternion.Euler(myTransform.rotation.x, Camera.main.transform.eulerAngles.y, myTransform.rotation.z));

But this may also cause you problems. There are multiple Euler angle representations for any physical representation. And the Euler angles you set are not necessarily the ones you get back out. For example do this:

 transform.eulerAngles = Vector3(180,0,0);
 Debug.Log(transform.eulerAngles);

The debug will output (0,180,180), the same physical rotation, but not the same angles. An you can see how the shift in representation could cause issues.

A common solution to your rotation problem is this:

 Vector3 camDir = Camera.main.forward;
 camDir.y = 0.0;
 transform.rotation = Quaternion.LookRotation(camDir);
Comment
Add comment · Show 10 · 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 Mayday556 · Jul 30, 2013 at 11:44 PM 0
Share

This fixed the rotation problem, and I must thank you very much for that, but now the player is rotated backwards and moving forward with no buttons pressed.

avatar image robertbu · Jul 31, 2013 at 12:21 AM 0
Share

Which solution are you using? If you are using the first one with $$anonymous$$oveRotation(), a read of the reference says that it is a delta rotation rather than an absolute rotation. So the code above will not work. If you need the physics of the $$anonymous$$oveRotation(), something like this should work (untested):

 Vector3 camDir = Camera.main.forward;
 camDir.y = transform.y;
 Quaternion q = Quaternion.FromToRotation(transform.forward, camDir);
 rigidbody.$$anonymous$$oveRotation(q);

If you don't need the physics, just use the second solution.

avatar image Mayday556 · Jul 31, 2013 at 12:23 AM 0
Share

I was trying to use the 3rd one but, i'll try the one you just told me now. Also, in the line:

camDir.y = transform.y;

isn't there supposed to be transform.rotation.y?

edit: Trying this one, the player will shake violently and the rotation will put him almost sideways.

avatar image robertbu · Jul 31, 2013 at 12:29 AM 0
Share

I misread your comment above. What I just gave you is unlikely to solve your problem. How do you have your model setup? For most things in Unity, the expectation is that when the rotation of the model is (0,0,0), the forward of the character is facing positive 'z' and the feet of the character point to '-y'. If your character is not setup this way, then most example rotation code you get on this list will not work. You can get around the issue by changing the model in a modeling program or by using empty game object with (0,0,0) rotation as a parent. Scripts go on the empty game object.

avatar image Mayday556 · Jul 31, 2013 at 12:36 AM 0
Share

Right now the player is only a capsule, I haven't started modelling yet, the rotation is set to (0, 0, 0). However I will try the empty parent thing.

Edit: Sigh Nope, nothing is working. I think I'll just have to use the given 3d Camera script with no readability

Show more comments

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

15 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

Related Questions

How to get rigidbody local z axis rotation in Euler angles? 0 Answers

Why can't I assign a rotation to a rigidbody Object? 1 Answer

How to make car land on 4 wheels and maintain speed? 0 Answers

eulerAngles.y always returns 179.9999 1 Answer

How to restrict rotation properly 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