Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Painstake · Aug 18, 2014 at 12:58 AM · javascriptraycastquaternioneulerroation

Rotating an object only a set amount of degrees

I have an object rotating continuously left/right, and would like to only have it rotate 50 degrees in each direction then stop. Is there a way to accomplish this?

Here is my current code:

 var speed : float;
 var lerpSpeed : float;
 private var xDeg : float;
 private var yDeg : float;
 private var fromRotation : Quaternion;
 private var toRotation : Quaternion;
 var cube1 : GameObject;
 var rearSightLayer : int = (1 << 15);
 
 function Update () {
     
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     
     if (Physics.Raycast(ray, hit, Mathf.Infinity, rearSightLayer) && hit.transform.tag == "RearSight3")
     {
         if(Input.GetMouseButton(0)) 
         {
             xDeg -= Input.GetAxis("Mouse Y") * speed;
             fromRotation = transform.rotation;
             toRotation = Quaternion.Euler(xDeg,0,0);
             cube1.transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.fixedTime * lerpSpeed);
         }
     }
     
 }
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 IgnoranceIsBliss · Aug 18, 2014 at 01:18 AM

You should make sure that your input to ANY interpolation function (lerp/slerp/smoothstep) is between 0 and 1. Yours will just keep going up and up, so you'd only ever move your head in one direction and it will stay there forever.

Something like

 float Amnt = Time.time / TotalTravel; 
    
 Quaternion.Slerp(fromRotation,toRotation,Mathf.Sin((Amnt - Mathf.Floor(Amnt)) * Mathf.PI));

Where 'TotalTravel' is how long (in seconds) you'd like it to take to do both passes.

NOTE: This is just off the top of my head and the math may not work - just keep the 'percentage' going into the interpolation function between 0 and 1.

Comment
Add comment · Show 2 · 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 IgnoranceIsBliss · Aug 18, 2014 at 01:19 AM 0
Share

The advantage of using 'Sin' like this is that it cycles infinitely anyway, and you get smooth starts and stops to your animation cycle without having to call $$anonymous$$athf.SmoothStep.

avatar image Painstake · Aug 18, 2014 at 03:42 PM 0
Share

Thanks for the info! Your code works just as well if not better than $$anonymous$$e, however I am still running into the same issue as before. The pass in each direction isn't set by an animation. The user clicks on the object to get it to rotate. Is there a way to pass in a stop point in degrees for each direction?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycasting at an offset angle? 4 Answers

How to raycast using a vector3 and a rotation? 1 Answer

Align plane to normal but keep Y-axis intact 1 Answer

GetComponent() problems 2 Answers

Raycasting fail 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