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 Phil74 · Apr 13, 2013 at 03:36 PM · animationrotationcube

Animate a cube rotation with script only

Hello guys ! I have a little problem here. I'm making a game (working already) where my character is moving on a side of a cube and make the cube to rotate when he's reaching a face limit (exactly as in this game http://armorgames.com/play/13357). The thing is everything is working but I can't figure out how to make the rotation animations. I tried Lerp Slerp but it don't do anything except freezing for two seconds and then rotating as if I was doing transform.Rotate(0, 90, 0); A little help please ? =) Thanks in advance !

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
1
Best Answer

Answer by Phil74 · May 03, 2013 at 09:18 PM

Just to let people know how I managed to resolve my problem, I'm using the animation component but i'm creating the curves in a script while the program is running. Here is a sample :

 public class CubeController : MonoBehaviour {
     
         Quaternion        resulting;
         Quaternion        beginning;
         AnimationClip    rotation;
         AnimationCurve    curve_x;
         AnimationCurve    curve_y;
         AnimationCurve    curve_z;
         AnimationCurve    curve_w;
         GameObject        cube;
         int                i;
 
 
     void Start ()
     {        
         cube = GameObject.Find("Map");
         rotation = new AnimationClip();
     }
     
     void Animate(Quaternion beginning, Quaternion resulting)
     {
         rotation.ClearCurves();
         curve_x = AnimationCurve.EaseInOut(0, beginning.x, 1, resulting.x);
         curve_y = AnimationCurve.EaseInOut(0, beginning.y, 1, resulting.y);
         curve_z = AnimationCurve.EaseInOut(0, beginning.z, 1, resulting.z);
         curve_w = AnimationCurve.EaseInOut(0, beginning.w, 1, resulting.w);
         rotation.SetCurve("", typeof(Transform), "localRotation.x", curve_x);
         rotation.SetCurve("", typeof(Transform), "localRotation.y", curve_y);
         rotation.SetCurve("", typeof(Transform), "localRotation.z", curve_z);
         rotation.SetCurve("", typeof(Transform), "localRotation.w", curve_w);
         cube.animation.AddClip(rotation, "rotate");
         cube.animation.Play("rotate");
     }
     
     void left()
     {
         beginning = cube.transform.rotation;        
         cube.transform.Rotate(0, -90, 0, Space.World);
         resulting = cube.transform.rotation;
         cube.transform.rotation = beginning;
         Animate(beginning, resulting);
     }
 }
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 Elsammak · Mar 25, 2014 at 12:57 PM 0
Share

Thanks dude, u saved me. You are the one ;)

avatar image
1

Answer by Unitraxx · Apr 13, 2013 at 04:05 PM

The most primitive way is to put this in the Update() function :

 transform.Rotate(0, rotateSpeed * Time.deltaTime, 0); 

Of course you need to define rotateSpeed, try a public variable so you can play with it in the inspector.

(You should add some math though, to determine when a total of 90 degrees has been rotated.)

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 Owen-Reynolds · Apr 13, 2013 at 04:21 PM 0
Share

Sometimes the Rotate&keepTrack method can leave your count at 90, but the real rotation at almost 90. If it does, can key off of your own R float variable and set directly from that:

transform.rotation = Quaternion.Euler(0,R,0);. But only works if you started facing dead ahead.

avatar image
0

Answer by Phil74 · Apr 14, 2013 at 07:36 PM

Thanks for the answers but I can't put it in the Update function because i'm calling my rotations functions in OnTriggerExit where i detect if i'm exiting from left, right, down, or up. I tried also to put a thread.sleep in a loop from 0 to 90 where i'm doing 1 degrees rotations but it does a one time 90° on screen. I tried to force the update with camera.render but it doesn't do anything. This is driving me crazy !

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 Owen-Reynolds · Apr 15, 2013 at 12:32 AM 0
Share

Look at movement lerps (many, many are here.) They mostly involve setting a target (which you would do in Exit,) then having Update creep to the target. It seems odd, to have Update always spin you, but after it reaches the angle, the lerp won't do anything.

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

13 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using Quaternions and C# script to animate a mesh 1 Answer

Raycast play animation1 and then animation2 1 Answer

Continously move an object with animation 1 Answer

Rotating An Object On Its Axis Once 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