Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Der_Kevin · Dec 10, 2015 at 02:29 PM · transformrotateraycasting

rotate a plane as long as if statement is true?

hi! i want to rotate a plane as long as the mouse raycasted a object. which looks like this now:

 if (Input.GetMouseButton(0)) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

             if (Physics.Raycast(ray, out hit, 100)) {
                 // whatever tag you are looking for on your game object
                 if(hit.collider.tag == "Trigger") { 
                     transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);

                 }
             }
                          
         }

and if you release the left button it should rotate back to zero:

 if (!Input.GetMouseButton(0)){
             transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);
                 }

so what i want to understand or like to know is, how can i let the plane rotate as long as the mouse button is down smoothly until a certain degree? (lets say 20°)

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 callen · Dec 10, 2015 at 03:08 PM

I hope this helps, but I don't fully understand what you're trying to do with that code. In both your mouse-down and mouse-up blocks, you use the same rotation code:

 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.identity, 0.15f);

This is mostly correct for when you want to return to zero rotation (that's what the Identity will give you), but to end somewhere else, say 20 degrees (along the z-axis), you could do something like this:

 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0,0,20), 0.15f);

But for your constantly-rotating logic, using Lerp isn't the best option. Instead, you would want to multiply the current rotation with another quaternion representing a small arc of the desired rotation, like this:

 //rotate the object one full revolution per second, around the z-axis
 transform.rotation *= Quaternion.Euler(0,0,360*time.deltaTime);

Hope this helps!

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 Der_Kevin · Dec 10, 2015 at 03:20 PM 0
Share

thanks! the second one was what i was looking for :) sorry for the weird explaination in the original post :)

avatar image Der_Kevin Der_Kevin · Dec 10, 2015 at 03:29 PM 0
Share

wait, actually i have another question: with

 transform.rotation *= Quaternion.Euler(0,0,20*Time.deltaTime); 

it rotates smoothly but all the time. but with

 transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0,0,20), 0.15f);

it rotates til 20° but not smooth. how can i let it rotate smooth and with this limit?

avatar image callen Der_Kevin · Dec 10, 2015 at 05:00 PM 0
Share

What the second line there is doing, is every frame it rotates 15% of the way between the current rotation and the 20 degree rotation. So for example, if you were starting at 120 degrees...

 Frame 1, rotate (120 - 20) * 0.15 = 15 degrees, and your new rotation is 105
 Frame 2, rotate (105 - 20) * 0.15 = 12.75 degrees, and new rotation is 92.25
 Frame 3, rotate (92.25 - 20) * 0.15 = 10.84 degrees, and new rotation is 81.41
 ...

This will keep bringing the angle towards 20, but each frame the angle rotated becomes smaller and smaller. After many frames the equation will look something like: Frame X, rotate (20.05 - 20) * 0.15 = 0.075 degrees, new rotation is 20.425

Now, with the 20*Time.deltaTime version, you're essentially (if the framerate is constant) moving the object the same angle each frame. The constant you multiply it by (20 in your example) means it will rotate that many degrees per second, whereas in my example I used 360 to give one full rotation per second. Whatever speed works best for you.

If what you're really after is a combination of these two - aka to move at a constant speed, until you get to a certain stop-point, you would use the constant-motion version and add a check along the lines of

 //declare a member variable named 'lastAngle' in the class
 transform.rotation *= Quaternion.Euler(0, 0, 360 * Time.deltaTime);
 if(lastAngle < 20 && transform.rotation.eulerAngles.z >= 20)
    enabled = false; //or another piece of logic to turn off the spinning effect
 lastAngle = transform.rotation.eulerAngles.z;

However, note that this approach has lots of gotchas - if your 'target' is small or 0, the "lastAngle < target" part might never be true. Also, your final angle may be slightly larger than the target (like, 20.03)... you should be able to get your basics working with all this though!

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

31 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

Related Questions

Turning faster right than left when using Rotate? 1 Answer

Applying two independent rotations 0 Answers

Problems With .rotate behavior 1 Answer

Rotor script (rotation into the - values) 1 Answer

Obtain a Transform using RayCastAll without a Collision (ie a point in space) 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