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 Sirmaiji · Sep 05, 2013 at 09:25 PM · rotationcontrollereuleranglesmathflimit

Rotation Limit

Here's a part of my code:

 function Update ()
 {
 var controller : CharacterController = GetComponent(CharacterController);
 transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
 var forward = transform.TransformDirection(Vector3.forward);
 var back = transform.TransformDirection(Vector3.back);
 var curSpeed = walkSpeed * Input.GetAxis ("Vertical");
 controller.SimpleMove(forward * curSpeed);
 
 if (Input.GetAxis("Horizontal") < 0.0){
        animation.CrossFade("turn_left");}
 
 if (Input.GetAxis("Horizontal") > 0.2){
        animation.CrossFade("turn_right");}
 
 if (Input.GetButton ("Run") && (Input.GetAxis("Vertical") < 0.0)){
        animation.CrossFade ("turn_back");
        walkSpeed = 0.0;
        transform.Rotate = (Time.deltaTime, 3.725, 0);


Actually what I need is to get character turn back 180 degrees fast when buttons "Run" + "Down" (Vertical < 0.0) pressed and then stop in default (idle) pose even if still those buttons are pressed. I dont want to limit rotation angles when obviously rotate left or right, but only when Run + Down pressed. So what do I need to add to this script to make it work? Thanks :)

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
1

Answer by aldonaletto · Sep 05, 2013 at 10:56 PM

If you want to rotate 180 degrees quickly (but not instantly) when the key is pressed, maybe a coroutine is the best solution. Care must be taken to not call the coroutine again while it's running, what can be done with the auxiliary boolean variable turningBack : it's set when the coroutine starts and cleared when it ends, and the code in Update must be blocked while turningBack is true. There's a pitfall however: rotating exactly 180 degrees is a problem because Unity may not identify about which axis you want to rotate (the object may rotate about very weird axes!) To avoid this problem, the coroutine splits the 180 degrees in two 90 degrees rotations done in sequence.

The code could be something like this:

 var turnBackTime: float = 0.5; // duration of turn back
 private turningBack = false; // true while turning back
 
 function TurnBack() // coroutine
 {
   turningBack = true; // don't call me - I'm turning back!
   var rot0 = transform.rotation; // initial rotation
   var rot1 = rot0 * Quaternion.Euler(0, 90, 0); // first 90 degrees
   var rot2 = rot1 * Quaternion.Euler(0, 90, 0); // last 90 degrees
   var rate: float = 2 / turnBackTime; // calculate the rate for 2 90 degrees rotations
   var t: float = 0; // control variable
   while (t < 1){ // rotate first 90 degrees
     t += rate * Time.deltaTime;
     transform.rotation = Quaternion.Slerp(rot0, rot1, t);
     yield;
   }
   t = 0; // do next 90 degrees rotation
   while (t < 1){
     t += rate * Time.deltaTime;
     transform.rotation = Quaternion.Slerp(rot1, rot2, t);
     yield;
   }
   turningBack = false; // rotation finished
 }
 
 function Update ()
 {
   // first change in Update: abort it while turning back
   if (turningBack) return;

   // the code below is the same as before:
   var controller : CharacterController = GetComponent(CharacterController);
   transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
   var forward = transform.TransformDirection(Vector3.forward);
   var back = transform.TransformDirection(Vector3.back);
   var curSpeed = walkSpeed * Input.GetAxis ("Vertical");
   controller.SimpleMove(forward * curSpeed);
   if (Input.GetAxis("Horizontal") < 0.0){
     animation.CrossFade("turn_left");
   }
   if (Input.GetAxis("Horizontal") > 0.2){
     animation.CrossFade("turn_right");
   }

   // here's the second modification in Update:
   if (Input.GetButtonDown("Run") && (Input.GetAxis("Vertical") < 0.0)){
     animation.CrossFade ("turn_back");
     walkSpeed = 0.0;
     TurnBack(); // start the coroutine
   }
   ...
Comment
Add comment · Show 4 · 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 Sirmaiji · Sep 06, 2013 at 12:59 PM 0
Share

Thank you so much for your feedback! I'm pretty sure it works just how it should - character rotates exactly 180 back and then stops. But here come two issues unfortunately: firs is that transition between "turn back" and "idle" animation is not smooth. As we get our coroutine TurnBack to false character's position changes sidewise slightly with a twitch at the end of turn back sequence. The second - rotation itself. Character turns around his axis but with a bit offset. As for rotation sequence, I guess we can make it much easier just by giving to a variable "rot1" range of, let's say, 179.9 y-rotation. Thereby there's no sequence needed at all. It works fine for me as far as there's no need in perfect mathematical precision actually :)

avatar image aldonaletto · Sep 06, 2013 at 04:19 PM 0
Share

Yes, using a single rotation slightly smaller than 180 solves the problem - the 90+90 solution is needed when you want exactly 180 degrees. About the offset: it seems that the pivot isn't exactly at the model center - or at least not at the position you want. This could be solved by childing the model to an empty game object - you should then rotate the empty, not the model, and the model position could be adjusted so that the rotation would work as expected.

avatar image Sirmaiji · Sep 06, 2013 at 08:34 PM 0
Share

I've managed to solve pivot issue, now everything works as it should! Thank you so much for your assistance! You're just awesome! :)

avatar image Sirmaiji · Sep 07, 2013 at 01:27 AM 0
Share

Unfortunately for now, after I've adjusted a pivot point by parenting character to empty object, I can't get him stop rotating after first cycle of turning back. What can be wrong?

avatar image
0

Answer by eddyzy · Sep 05, 2013 at 10:12 PM

why don`t you use the mecanim animation system?

take a look at Quaternion.Slerp and at transform.Rotate http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html

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

17 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

Related Questions

accelerometer Rotation limiting 1 Answer

Find the difference between the Z rotation of two GameObjects 2 Answers

limit accelerometer controlled rotation 1 Answer

Mathf.SmoothDamp freeking out? 0 Answers

Get an eular angle between 0 and 360 after calculations 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