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 NickMx · Mar 06, 2014 at 06:11 PM · rotationquaternionrotateslerplerpz

This can't be impossible or? Rotate (flip) smoothly from one point to endpoint on mouseclick.

I've tried to follow all examples in the script references, about Quaternion, euler.angles, lerp and slerp, transform.Rotate vs transform.rotation, but can't get the effect I want. I can get a cube to spin around it's axis, I can get it to rotate smoothly under update (in several ways), but as soon as I try to connect that movement to a mouse click, it fails. The movement halts before the endpoint or flicker.

I don't know if it's related to Time.deltatime or Update or what.

This is what I want, example: On a Rayhit or Mouseclick; A flat Cube flipping over from, say 0 to 90 degrees on one axis, and with just some smoothness in the movement.

These two alternative codes both do some of the job but not completely:

The motion/animation is right in this code if I just put it under Update: { transform.rotation=Quaternion.identity;transform.rotation = Quaternion.Slerp(Startpoint.rotation, Endpoint.rotation, Time.time * speed); } (The variables startpoint and endpoint are set as public transforms and start is 0,0,0 in rotation and endpoint as 0,0,90)

This code gets the position and clicking right, but I can't get any smoothness/animation here, it will move instantly of course.

void OnMouseDown() { transform.Rotate(new Vector3(0,0,90)); } I tried to adjust my codes to some tips from links and tutorials, even setting a division sign before time.deltatime, (one link I suspect is related is this http://answers.unity3d.com/questions/200115/why-is-my-camera-shaky-in-editor-but-not-in-compil.html), I tried using Quaternions, euler.angles, defining variables and functions to try to get it right, just no luck.

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

3 Replies

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

Answer by giano574 · Mar 06, 2014 at 07:49 PM

You can use Quaternion.RotateTowards. Have a boolean set to true when the mouse is clicked and then use the function in the Update method:

 if (rotate == true)
 {
    transform.rotation = Quaternion.RotateTowards(transform.rotation, 
                                                  Quaternion.eulerAngles(0,0,90), 
                                                  speed * Time.deltaTime);
  }

The RotateTorwards function will return a Quaternion that is speed * Time.deltaTime closer to the target rotation. As stated in the script reference the rotation will not overshoot.

See: https://docs.unity3d.com/Documentation/ScriptReference/Quaternion.RotateTowards.html

Comment
Add comment · Show 1 · 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 NickMx · Mar 06, 2014 at 11:23 PM 0
Share

Thank you so much, I finally got it working! It rotates so nice! I recognize that rotateTowards vaguely, must have been too tired to pay properly attention to it. I managed now when making mouseclick turn a bool to true. (I added sort of "not true" factor also in the statement so the Update don't need to check all the time (but that's what update do but you know what I mean) perhaps one need that in some cases.

if (rotate== true && transform.rotation!= Endpoint.rotation) { transform.rotation = Quaternion.RotateTowards(transform.rotation, Endpoint.rotation, step); Debug.Log("if rotate is true"); }

avatar image
0

Answer by fifthknotch · Mar 06, 2014 at 06:27 PM

Under the Update function in your first script add the if statement

 if (Input.GetMouse(0) {
 //rest of code
 }

"it will move instantly of course." OnMouseDown only happens the frame the mouse button was clicked. It is not continuous. In your code, you tell the object to rotate 90 degrees instantly, that is why your rotation is correct but animation is not. Use instead OnMouse. It will happen every frame the mouse is down.

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 NickMx · Mar 06, 2014 at 07:35 PM 0
Share

Good to know, thanks! At least now I can make the cube spin around (forever) by holding the mouse down, on the script with transform.Rotate. However with the other script, I still can't make it slow down in movement, I tried to tweak the speed float, and/or change the time type, it either stops before the endpoint or just moves instantly. And I would like to accomplish this with just one mouseclick, that is, click on the object, and it flips to the new position in a second or so. Perhaps I need to turn to an true animation solution here?

avatar image NickMx · Mar 06, 2014 at 07:52 PM 0
Share

After whatching this tutorial (even though it's java, http://www.youtube.com/watch?v=kvgm0QBtZJw) I got it a little more right, by removing my original float and time.time or time.deltatime, put the float time += in ins$$anonymous$$d (and added as a variable at the beginning). Now it moves nice when holding down the mouse. But as I said I would like to accomplish it with just one $$anonymous$$ouseclick. Perhaps I can convert it to a function in some way...

void Update() { time += 0.01F; if (Input.Get$$anonymous$$ouseButton(0))

 {
         transform.rotation=Quaternion.identity;transform.rotation = Quaternion.Slerp(Startpoint.rotation, Endpoint.rotation, time);
         
 }
avatar image fifthknotch · Mar 07, 2014 at 06:00 AM 0
Share

Aw I did not realize you wanted it in one click. Yes giano's method is best.

avatar image
0

Answer by robertbu · Mar 06, 2014 at 09:34 PM

Here is a bit of example code. It rotates the cube around the world 'y' axis. Each time it is clicked, it adds 90 degrees to the rotation. If you want eased movement at the end, change RotateTowards() to Slerp() and reduce the value of 'speed' in the Inspector.

 #pragma strict
 
 var qTo : Quaternion;
 var speed = 45.0; 
 
 function Start() {
     qTo = transform.rotation;
 }
 
 function Update() {
     transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);
 }
 
 function OnMouseDown() {
     qTo = Quaternion.AngleAxis(90.0, Vector3.up) * qTo; 
 }

When used with RotateTowards(), 'speed' is degrees per second.

Comment
Add comment · Show 1 · 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 NickMx · Mar 06, 2014 at 11:25 PM 0
Share

Thanks! This alternative is also good. I did not know about this AngleAxis, this ter$$anonymous$$ology with Quaternions, Eulers and all can really be tough to know when and how to apply. (I can't upvote these answers yet, I'm too new, and it also seems like you can only mark one as answer, but I think they both give good working alternatives.)

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

22 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

Related Questions

Quaternion.Slerp problem... 1 Answer

How to smoothly rotate an object on only two axes? 2 Answers

How can you make a nav mesh agent rotate 180 degrees? 1 Answer

Use Lerp to rotate object 2 Answers

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 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