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 LucasMars · Jul 29, 2013 at 02:44 PM · transformrotateboolean

Rotation Help

I have some code that I want a GameObject and I want it to rotate 4 degrees first and then -4 degrees after the 4 degrees is finished. I have some code which I think should work...

 var xrot : float;
 var bool : boolean;
 var speed : float;
 
 function Start(){
     InvokeRepeating("run", 0, 0.01);
     var otherscript : Leg_Move_02;
     var thisscript : Leg_Move;
 }
 
 function run() {
     if(bool==true){
         xrot -=speed;
     }
     
     if(bool==false){
         xrot +=speed;
     }
     
     if(xrot >4){
         bool=true;
     }
     
     if(xrot <-4){
         bool=false;
     }
     
     transform.Rotate(xrot, 0, 0);
 }

But for some reason, it gets to 0 and then goes back to 4 and not -4. Why is this happening?

Any Answers Are Appreciated

[EDIT] I decided to put in an example.

1) Create a new scene and add a gameobject in front of the camera

2) Create a cube and place that inside the gameobject

3) Add the script above to the gameobject and change the var speed (in the editor) to 0.25

4)You will see what I mean.

[/EDIT]

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 brandonsbarber · Jul 29, 2013 at 03:04 PM 0
Share

So you want it to go between 4 and -4? Essentially a full pendulum?

avatar image LucasMars · Jul 29, 2013 at 03:07 PM 0
Share

I want it to go between to values (I've chose 4 because it has been working the best) that cross 0.

2 Replies

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

Answer by robertbu · Jul 29, 2013 at 03:14 PM

@Kristian has the right idea. transform.Rotate() is a relative rotation. So each time you call it, you are not rotating to the specified degrees, but adding more rotation each frame. If you want it to just rotate from -4 to 4 degrees, you can do this:

 #pragma strict
 var xrot : float;
 var bool : boolean;
 var speed : float= 0.1;
 
 var v3Rot = Vector3.zero;
  
 function Start(){
     InvokeRepeating("run", 0, 0.01);
     var otherscript : Leg_Move_02;
     var thisscript : Leg_Move;
 }
  
 function run() {
     if(bool==true){
        xrot -=speed;
     }
  
     if(bool==false){
        xrot +=speed;
     }
  
     if(xrot >4){
        bool=true;
     }
  
     if(xrot <-4){
        bool=false;
     }
     
     v3Rot.x = xrot;
     transform.eulerAngles = v3Rot;
 }
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 LucasMars · Jul 29, 2013 at 03:16 PM 0
Share

Thanks! That was what I was looking for!

avatar image
1

Answer by Kristian · Jul 29, 2013 at 03:04 PM

Well, one problem I see here is that you rotate with xrot every cycle. Meaning that you will rotate speed*0.01 then speed*0.02 then speed*0.03 etc. Resulting in a rotation of approximately 800 degrees ((4/0.01) * (4/2)).

About your other issue, I'm not sure I'm getting the issue, it seems understandable that it should start as 0 and then go to 4, as it first enters the (xrot += speed) line, due to the bool starting as false by default.

I hope this helped in some way :)

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

18 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

Related Questions

i want an object rotate 45 degrees more than an other one 1 Answer

Avatar Rotation Control [UnityScript] 0 Answers

How to rotate without a target vector 1 Answer

Orientation Vs. Rotation: How do I specify more than 360deg rotation for a quaternion, in the editor? 1 Answer

[Help] Object flips around when using transform.Rotate 3 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