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 Kevin_C · May 19, 2012 at 02:11 PM · rotationcorrection

keeping the player upright

In my game I want to have the player able to go up inclined surfaces (I know that there is a limit to angle, and such), but I have the problem that when the angle of the player breaks a 45 it starts free spinning, and can easily end up upside down. I have implemented a input.GetKey to put the character right side up, but I need to figure out how to have a script do it automatically.

when I try this my character doesn't rotate at all. Note: on reset correction Y-rotation is preserved.

 //current solution (character will not rotate at all about the x, or z axes.)
 if((transform.rotation.eulerAngles.x<-180)||(transform.rotation.eulerAngles.x>180)||
     (transform.rotation.eulerAngles.z<-180)||(transform.rotation.eulerAngles.z >180)){
         // preserve Y rotation
         float currentLook = transform.rotation.eulerAngles.y;
         // put character to default rotation
         transform.rotation = Quaternion.identity;
         // re-introduce Y rotation
         transform.Rotate(0,currentLook,0);
 }
 // works as intended
 if(Input.GetKey(".")){
     // preserve Y rotation
     float currentLook = transform.rotation.eulerAngles.y;
     // put character to default rotation
     transform.rotation = Quaternion.identity;
     // re-introduce Y rotation
     transform.Rotate(0,currentLook,0);
 }

EDIT: currently I have my character wrapped in a box collider, and I would like to have it so that it can go up inclined planes, but I don't want to have the player flip over.

does anyone have any ideas on how to keep the player from flipping over (might be using same logic for my AI agents if they have the same problem)

  • I still want the player to be able to rotate with respect to all axes,

  • once the transform breaks 90 with respect to either x, or z to have it go back to identity (then rotate by the previous y rotation)

  • and possibly let collision resolution(gravity) put it back to where it needs to be.

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 Kevin_C · May 25, 2012 at 07:32 PM

Performed test, and found that the understanding I was using for the eulerAngles was not the actual behavior.

The behavior of eulerAngles is such that they are only values from [0,360), and any negative(clockwise with respect to the give axis) that would make it go negative will instead place it to 360, and then start reducing. to demonstrate this the following code can be applied to a camera (a "Rotation" input axis would need to be introduced)

 using UnityEngine;
 using System.Collections;

 public class PlayerController : MonoBehaviour {
     
     public float rotationScale = 40;
     
     // Update is called once per frame
     void Update () {
         float foward = Input.GetAxis("Vertical")  *Time.deltaTime*rotationScale;
         float side   = Input.GetAxis("Horizontal")*Time.deltaTime*rotationScale;
         float rot    = Input.GetAxis("Rotation")  *Time.deltaTime*rotationScale;
         transform.Rotate(foward,rot,side);
         print (transform.eulerAngles.ToString());
     }
 }

So the corrected code from the question would be:

 if(((transform.rotation.eulerAngles.x<270)&&(transform.rotation.eulerAngles.x>90))||
     ((transform.rotation.eulerAngles.z<270)&&(transform.rotation.eulerAngles.z>90))){
         // preserve Y rotation
         float currentLook = transform.rotation.eulerAngles.y;
         // put character to default rotation
         transform.rotation = Quaternion.identity;
         // re-introduce Y rotation
         transform.Rotate(0,currentLook,0);
 }

Will keep the player from passing a 90 degree rotation with respect to those 2 axis, though in some situations it might be beneficial to break these apart instead of using a logical or (`||`) to minimize the need to have collision resolution clean up after the fact.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Help to figure a logic to achieve a movement. 2 Answers

Basic quaternion question 1 Answer

Rotating An Object On Its Axis Once 3 Answers

rotating an object only on z-axis help - c# 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