• 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 /
  • Help Room /
avatar image
0
Question by Dragondean · Jan 04 at 11:20 PM · c#scripting problemtransform3dquaternion

Slerp Rotating 1 Degree and Limiting All Other Rotation

The title may be sort of confusing, but let me explain.


So I'm building a game in which you can flip your local gravity, not the global, but your gravity and yours alone. I got the gravity flipping correct but having you immediately snap to 180 degrees z is kinda disorienting.


So, I've been trying to make a function that slowly rotates the player. There's obviously something wrong with my slerp function as that's where the actual rotation lies. But, I'm only rotating one degree on the z axis and I cant rotate on any other axis, ie looking around. Here is a my code: //Flip Input if (Input.GetButton("Flip")) gravFlipInput = true; else if (Input.GetButtonUp("Flip")) gravFlipInput = false;

         //Check If Flip Is Possible and If So, Flip
         if (gravFlipInput && !flipping && Grounded())
         {
             flipping = true;
             turning = true;
             DIRECTION = DIRECTION ? false : true;
             flipGrav = DIRECTION ? -1 : 1;
         }
 
         //Check if Flip is Completed
         if (flipping && Grounded())
             flipping = false;
 
         //Actual Flip Process
         if (turning)
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z + 180)), flipInterpolationRatio);
 
         if (turning && (transform.rotation.z == 180 || transform.rotation.z == 0) && flipcancel)
         {
             turning = false;
             flipcancel = false;
         }
 
    //prevents noting starting rotation
     IEnumerator FlipCancel()
     {
         yield return new WaitForSeconds(1);
         flipcancel = true;
     }
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

Answer by xxmariofer · Jan 05 at 09:03 AM

Well that code dont make much sense so my suggestion is to try to refactor it using coroutines, but here, if the rest of the code is fine, should be an easy fix

 if (Input.GetButton("Flip")) gravFlipInput = true; 
 else if (Input.GetButtonUp("Flip")) gravFlipInput = false;
 
 //Check If Flip Is Possible and If So, Flip
 if (gravFlipInput && !flipping && Grounded())
 {
     flipping = true;
     turning = true;
     StartCoroutine(Flip())
     DIRECTION = DIRECTION ? false : true;
     flipGrav = DIRECTION ? -1 : 1;
 }
 
 //Check if Flip is Completed
 if (flipping && Grounded())
     flipping = false;
 
 if (turning && (transform.rotation.z == 180 || transform.rotation.z == 0) && flipcancel)
 {
     turning = false;
     flipcancel = false;
 }
 
 IEnumerator Flip()
 {
     Quaternion target = Quaternion.Euler(new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z + 180));
     while (turning)
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, target, flipInterpolationRatio);
     }
 }
 
 //prevents noting starting rotation
 IEnumerator FlipCancel()
 {
     yield return new WaitForSeconds(1);
     flipcancel = true;
 }


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

217 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 avatar image avatar image

Related Questions

Preventing A Teleporting GameObject From Passing Through Walls 2 Answers

Rotation set to face direction of movement but doesn't always work 1 Answer

Randomly set an integer as positive or negative? 1 Answer

How to change rotation for a FirstPersonController through script. 0 Answers

Flip 3D Character Rotation 180 on Y axis 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges