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 N1nja · Jun 18, 2012 at 05:54 PM · rotationdirectionlookatnormalup

Proper Variation of this code, to work with any UP normal;

             if(newVelocity.magnitude > 0) {
             Vector3 up = transform.up;
             Vector3 desiredLookPos = seekPos;
             desiredLookPos.y = transform.position.y;
             Vector3 desiredLookDir = desiredLookPos - transform.position;
             desiredLookDir.Normalize();
             float rotationDeltaInRadians = m_rotationSpeed * Time.deltaTime;
             Vector3 newForwardDirection = Vector3.RotateTowards(
                 transform.forward, 
                 desiredLookDir, 
                 rotationDeltaInRadians,
                 0.0f
             );
             transform.forward = newForwardDirection;
             transform.up = up;
         }

So, basically the above code works all fine and well, minus the transform.up located at the bottom, was a temperary fix to something;

anyways, basically the character is suppose to rotate towards the direction of a point, and its working quite well, however I come to a point where my character has an upside down UP axis -1, instead of 1, and basically, that messes everything up; because now he rotates the opposite direction while upside down, where as when up-right he doesnt rotate at all(at least since I added the transform.up = up; )

is there anyone on here who can help me out of this pickle, and get me the right calculations to make him rotate problem, no matter if his UP normal is 0,1,0; or 0,-1,0 ?? please!

would be a great thing, and I suppose I can even offer a $5 reward.

Comment
Add comment · Show 2
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 whydoidoit · Jun 18, 2012 at 06:37 PM 0
Share

Just to be sure - you are sure that newVelocity.magnitude is > 0? m_rotationSpeed is sensible etc and it doesn't work even if max change in magnitude is set to a large value (as your magnitude isn't changing anyway, just to test).

avatar image N1nja · Jun 18, 2012 at 08:08 PM 0
Share

he rotates fine.. just not when I set his up axis to 0,-1,0; then he rotates oppositely, otherwise he does not rotate at all.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by N1nja · Jun 18, 2012 at 10:17 PM

                 var up = transform.up;
             var lookRot = Quaternion.LookRotation((seekPos - transform.position)*up.y);
             var axisAngles =transform.rotation.eulerAngles;
             axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed *   Mathf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
             transform.rotation = Quaternion.Euler(axisAngles);

this appears to have fixed it. Mike was the closest, so Mike, if you read this again, let me know and I will give you $5.00 :D Just need your paypal email.

Comment
Add comment · Show 3 · 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 whydoidoit · Jun 18, 2012 at 10:21 PM 0
Share

Hey man, don't need your cash, just glad you solved the problem :)

avatar image N1nja · Jun 18, 2012 at 10:26 PM 0
Share

fine. i do not need to give it to you then. xP

thanks for the input, u just had to move that * up.y up a few lines.. don't ask why I thought to do that, other then I knew that would also invert the direction from the start, hoping that'd be enough.

turns out it was. :P

avatar image whydoidoit · Jun 18, 2012 at 10:29 PM 0
Share

Yeah - saw that - makes sense NOW ;)

avatar image
1

Answer by whydoidoit · Jun 18, 2012 at 09:01 PM

   var up = transform.up;
   var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
   var axisAngles =transform.rotation.eulerAngles;
   axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed *   Mathf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
   transform.rotation = Quaterion.Euler(axisAngles);
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 N1nja · Jun 18, 2012 at 09:38 PM 0
Share

$$anonymous$$uch closer, however while upside down, he no longer looks forward at me, he turns around and his back looks forward at me, this was the original problem, that I am trying to fix. Good though.

avatar image whydoidoit · Jun 18, 2012 at 09:43 PM 0
Share

There has to be a better way than this :)

   var up = transform.up;
   var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
   var axisAngles =transform.rotation.eulerAngles;
   axisAngles.y = Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed *   $$anonymous$$athf.Rad2Deg * Time.deltaTime)).eulerAngles.y;
   transform.rotation = Quaterion.Euler(axisAngles);
   transform.forward = transform.forward * up.y;
avatar image N1nja · Jun 18, 2012 at 10:15 PM 0
Share

i've tried the transform.forward * up.y myself, it doesn't work either.

This is a much harder problem then it seems like, trust me, I've tried most of these solution you've all posted. They are not working.

This may take some outside thinking.

avatar image whydoidoit · Jun 18, 2012 at 10:20 PM 0
Share

I guess I can't visualize the effect you after. I'm not sure what the existing rotations in X & Z are about. Can you describe the effect in terms of what your objects are doing? I guess you not being able to fix it with all the code and the idea is going to make it tricky for us with just a few lines of code and a guess at what you want ;) Does the character flip vertically by rotating around Z?

avatar image
0

Answer by aldonaletto · Jun 18, 2012 at 06:38 PM

I don't know why it's rotating to the wrong side when the object is upside down, but probably the version below may solve the problem - it uses Quaternion.RotateTowards instead of Vector3.RotateTowards, thus can be applied directly to the object rotation:

    if(newVelocity.magnitude > 0) {
         Vector3 desiredLookPos = seekPos;
         desiredLookPos.y = transform.position.y; // kill any height difference
         // find the desired rotation
         Quaternion lookRot = Quaternion.LookRotation(desiredLookPos - transform.position);
         // and rotate a little in its direction each pass 
         transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRot, m_rotationSpeed * Time.deltaTime);
    }
Notice that m_rotationSpeed in this case is expressed in degrees per second (don't know if the previous one was in radians/second)

EDITED: My bad! Quaternion.LookDir really doesn't exist: it should be Quaternion.LookRotation - answer fixed.

Comment
Add comment · Show 6 · 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 N1nja · Jun 18, 2012 at 06:43 PM 0
Share

Im sure this may work if Quaternion.LookDir was a real function, did you mean setLookRotation?

avatar image N1nja · Jun 18, 2012 at 06:47 PM 0
Share

I changed it to LookRotation, and it acts a little more reasonably, however hes rotation on the Z axis and X axis, how-come? Hard to say if its correct due to that, can you write it so it ensures he only rotates on the Y-axis, and in any UP normal?

also provide an email to paypal, if it works as expected, you will receive $5.00

avatar image whydoidoit · Jun 18, 2012 at 07:00 PM 0
Share

Hang on - you only want him to rotate about Y? You didn't mention that :)

   var lookRot = Quaterion.LookRotation(desiredLookPos - transform.position);
   var axisAngles =Quaternion.RotateTowards(transform.rotation, lookRot, (m_rotationSpeed * $$anonymous$$athf.Rad2Deg * Time.deltaTime)).eulerAngles;
   axisAngles.x = 0;
   axisAngles.z = 0;
   transform.rotation = Quaterion.Euler(axisAngles);
avatar image N1nja · Jun 18, 2012 at 08:07 PM 0
Share

The above rotates correctly, and only one the Y axis, as requested, but again, it does not keep the same UP Normal, as defined before the rotation occures. For instance before rotation Up Normal = [0, -1, 0]; after [0, 1, 0]; Where that shouldn't happen after, it should remain as before.

Providing an correct script, and email, will give you $5.00 first to do it right.

avatar image whydoidoit · Jun 18, 2012 at 08:13 PM 0
Share

Oh does it does, doesn't it - does it work if you just remove the axisAngles.z = 0 or perhaps it is more complicated than that...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating object to face target, while being rotated to match terrain slope 1 Answer

LookAt on only 1 axis for 2D games? 1 Answer

(CharacterController's) transform.Lookat not working as expected 1 Answer

Quaternion.LookRotation is making my object rotate around a point? 1 Answer

Adjust camera rotation while looking at a target 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