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 Fewpwew130 · Sep 16, 2015 at 10:17 AM · rotationcube

Rotate cube as if it were floating in the sea

Hello, dear community!

I'm trying to make a cube seem floating on a surface, I've found a script here: http://answers.unity3d.com/questions/822484/a.html

      public Vector3 from = new Vector3(0f, 0f, 135f);
      public Vector3 to   = new Vector3(0f, 0f, 225f);
      public float speed = 1.0f; 
      
      void Update() {
          float t = Mathf.PingPong(Time.time * speed * 2.0f, 1.0f);
          transform.eulerAngles = Vector3.Lerp (from, to, t);
          
      }

It works great! But it only "lerps" between 2 vectors. Is it possible to lerp between 4 or 5 different cube rotations? The help is appreciated!

EDIT: I've tried writing the following script, but it doesn't work: "Cannot implicitly convert float to int" in the "transform.eulerAngles = Vector3.Lerp (fromarrays[randomnumber1from], toarrays[randomnumber2to], t);"

 using UnityEngine;
 using System.Collections;
 
 public class rotation12 : MonoBehaviour {
     
         public Vector3 from = new Vector3(0f, 0f, 135f);
         public Vector3 to   = new Vector3(0f, 0f, 225f);
 
         public Vector3 from2 = new Vector3(0f, 0f, -100f);
         public Vector3 to2   = new Vector3(0f, 0f, 100f);
         public Vector3 from3 = new Vector3(0f, 0f, -200f);
         public Vector3 to3   = new Vector3(0f, 0f, 200f);
         public Vector3 from4 = new Vector3(0f, 0f, -300f);
         public Vector3 to4   = new Vector3(0f, 0f, 300f);
 
         public Vector3[] fromarrays;
         public Vector3[] toarrays;
 
         public float randomnumber1from;
         public float randomnumber2to;
 
         public float speed = 1.0f; 
         
     void Start(){
         fromarrays[0]=from;
         fromarrays[1]=from2;
         fromarrays[2]=from3;
         fromarrays[3]=from4;
 
         toarrays [0] = to;
         toarrays [1] = to2;
         toarrays [2] = to3;
         toarrays [3] = to4;
     }
 
     void Update() {
     
         rotatefuncrion ();
     }
 
     void rotatefuncrion(){
         float t = (Mathf.Sin (Time.time * speed * Mathf.PI * 2.0f) + 1.0f) / 2.0f;
         randomnumber1from = Random.Range (0, 3);
         randomnumber2to = Random.Range (0, 3);
         transform.eulerAngles = Vector3.Lerp (fromarrays[randomnumber1from], toarrays[randomnumber2to], t);
     }
         
 }
 

Could use suggest what is the reason?

Comment
Add comment · Show 6
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 Fewpwew130 · Sep 17, 2015 at 03:44 PM 0
Share

any ideas? :)

avatar image Glurth Fewpwew130 · Sep 17, 2015 at 03:56 PM 1
Share

Your error is due to the fact that you declared randomnumber1from, and randomnumber2to, as float. If you want to use them to index into an array, you must declare them as int.

But, I would suggest a different approach: why not generate a random angle to rotate "to", rather than use arrays?

 Vector3 to = new Vector3(0,0, Random.Range (0,360));
avatar image Fewpwew130 Glurth · Sep 17, 2015 at 04:13 PM 0
Share

Hey! Thank you for a reply, I cannot find an Upvote button, new forums require some time to get used to a new style. EDIT: found an upvote button

I've rewritten the code, it works almost as I wanted, except it does it many times per second. Is there a way to stop Random.Range till previous Lerp is done?

Here is the code.

 using UnityEngine;
 using System.Collections;
 
 public class rotation12 : $$anonymous$$onoBehaviour {
     
         public Vector3 from = new Vector3(0f, 0f, 135f);
         public Vector3 to   = new Vector3(0f, 0f, 225f);
 
         public Vector3 from2 = new Vector3(0f, 0f, -100f);
         public Vector3 to2   = new Vector3(0f, 0f, 100f);
         public Vector3 from3 = new Vector3(0f, 0f, -200f);
         public Vector3 to3   = new Vector3(0f, 0f, 200f);
         public Vector3 from4 = new Vector3(0f, 0f, -300f);
         public Vector3 to4   = new Vector3(0f, 0f, 300f);
 
         public Vector3[] fromarrays;
         public Vector3[] toarrays;
 
         public int randomnumber1from;
         public int randomnumber2to;
 
         public float speed = 1.0f; 
         
     void Start(){
         fromarrays = new Vector3[4];
         fromarrays[0]=from;
         fromarrays[1]=from2;
         fromarrays[2]=from3;
         fromarrays[3]=from4;
 
         toarrays = new Vector3[4];
         toarrays [0] = to;
         toarrays [1] = to2;
         toarrays [2] = to3;
         toarrays [3] = to4;
     
     }
 
     void Update() {
 
 
     
 
         rotatefunction ();
 
     }
 
     void rotatefunction(){
         float t = ($$anonymous$$athf.Sin (Time.time * speed * $$anonymous$$athf.PI * 2.0f) + 1.0f) / 2.0f;
         randomnumber1from = Random.Range (0, 4);
         randomnumber2to = Random.Range (0, 4);
         transform.eulerAngles = Vector3.Lerp (fromarrays [randomnumber1from], toarrays [randomnumber2to], t);
 
     }
 }
 
Show more comments
avatar image Fewpwew130 · Sep 17, 2015 at 06:00 PM 0
Share

Thank you very much! Is it possible to reward a user? I don't figured out all the possibilities of the new forums.

avatar image Glurth Fewpwew130 · Sep 17, 2015 at 07:37 PM 1
Share

Those little up & down chevrons (Sargent stripes) that appear when you mouse over a comment can be used to upvote a comment. I'll create a new answer that combines both comments, then you can click "Accept" to mark it as "the answer".

1 Reply

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

Answer by Glurth · Sep 17, 2015 at 07:37 PM

Your error is due to the fact that you declared randomnumber1from, and randomnumber2to, as float. If you want to use them to index into an array, you must declare them as int.

"Is there a way to stop Random.Range till previous Lerp is done?" Well, the parameter you pass to Lerp should be a value between 0 and 1. When it reaches 1, the Lerp could be considered "done" as it has reached the "to" Vector. In this case, I would compare t>0.95 in-case of rounding/timing problems

Worth noting, that since you are passing a cyclical sine wave, you might consider it "done" after a full "cycle", rather than simply when the value reaches 1. In this case, you will need to remember what "t" value your "cycle" started on, and wait till "t" it gets close to that number again. (create & use a class member variable to store this value, so it remains "in-scope" across multiple calls to update().)

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

30 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

Related Questions

How to calculate and use new direction angle as a new forward direction. 1 Answer

I need help in rotating cube by 90 degree with swipe in all axis 0 Answers

Instantiate bullet Impact Effect on cube , rotation problem 1 Answer

Rotating a cube in 3D ,Rotating a cube into specific coordinates with given input 0 Answers

Unity Rotating a Cube 0 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