• 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 OJ3D · Feb 18, 2014 at 04:54 AM · c#rigidbodyrotateexcel

how can I rotate object reading from excel data & time steps

Hey guys, I could really use some help here ( I apologize for being a noob in advance :| . Kinda new to Unity scripting and have a basic understanding of programming). Thanks for help in advance.

I'm trying to right a script ( C# preferably, but java can help) to grab data from excel. T$$anonymous$$s data is pretty much feeds angles of an object that rotates about the x axis. I need the object to rotate smoothly to those degrees. T$$anonymous$$nk of somet$$anonymous$$ng oscillating to the left, back, then to the right using these angles per time step.

When I do a test code with transform.rotate , it keeps rotating an my object continuously. I even looked into euler angles and that's slightly over my head. Anyways, I including a schematic and a sample excel table wit$$anonymous$$n the zip consisting of an angle of interest, the current angle, and incremental change....

using UnityEngine; using System.Collections;

public class Rotate Barge : MonoBehaviour {

 // Use t$$anonymous$$s for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {

 transform.rotate(0,0,0);
 //transform.rotate(1,0,0);
 //transform.rotate(2,0,0);
 
 }
  
 

}

link text

oscilating rotation.png (37.1 kB)
barge rotate.zip (73.9 kB)
Comment
Add comment · Show 3
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 RudyTheDev · Feb 18, 2014 at 12:28 PM 0
Share
avatar image Graham-Dunnett ♦♦ · Feb 18, 2014 at 12:36 PM 0
Share
avatar image OJ3D · Feb 19, 2014 at 12:33 AM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DerWoDaSo · Feb 18, 2014 at 01:17 PM

I would also use CSV files instead of Excel files, the way Graham mentioned in the comment.

Smooth interpolation could easily be done by creating an animation curve from script and adding these values from the CSV as keyframes.

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
avatar image
0

Answer by robertbu · Feb 19, 2014 at 01:20 AM

  • for @DerWoDaSo's suggestion of using an AnimationCurve. When I looked at your question yesterday, I saw a messy waypoint-like solution, but an AnimationCurve makes t$$anonymous$$ngs simple. I'd written CSV parsing code as an answer a couple of days ago and wanted to see what your rocking data would look like when played. So here is a class that will read in a CSV file of your Excel data and rocks the game object on the X axis. Note if you are going to have other rotations, you will need to either split t$$anonymous$$s rotation off to use its own game object or combine that code into t$$anonymous$$s class.

    using UnityEngine; using System.Collections;

    public class RockTheBoat : MonoBehaviour {

       private AnimationCurve ac = new AnimationCurve();
         private float maxTime;
     
         void Start() {
             LoadData("e:/data.csv");
             StartCoroutine(DoTheRocking(true));
         }
             
         private void LoadData(string filePath) {
             
             string input = System.IO.File.ReadAllText (filePath);
             string[] lines = input.Split (new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
     
             for (int i = 0; i < lines.Length; i++) {
                 string[] nums = lines[i].Split(new[] { ',' });
                 if (nums.Length < 2) {
                     Debug.Log ("Misforned input on line "+i+1);
                 }
                 float timestamp;
                 float angle;
                 if (float.TryParse (nums[0], out timestamp)) {
                     if (float.TryParse (nums[1], out angle)) {
                         ac.AddKey (timestamp, angle);
                         if (timestamp > maxTime)
                             maxTime = timestamp;
                     }
                 }
             }
         }
     
         private IEnumerator DoTheRocking(bool repeat) {
             do {
                 float time = 0.0f;
                 w$$anonymous$$le (time <= maxTime) {
                     transform.eulerAngles = new Vector3(ac.Evaluate (time), 0.0f, 0.0f);
                     yield return null;
                     time += Time.deltaTime;
                 }
             } w$$anonymous$$le (repeat);
         }
     }
    
Comment
Add comment · Show 5 · 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 mojojojo · Feb 20, 2014 at 02:30 AM 0
Share
avatar image mojojojo · Feb 20, 2014 at 02:39 AM 0
Share
avatar image robertbu · Feb 20, 2014 at 02:49 AM 1
Share
avatar image robertbu · Feb 20, 2014 at 05:18 AM 0
Share
avatar image DerWoDaSo · Feb 20, 2014 at 03:38 PM 0
Share
avatar image
0

Answer by OJ3D · Feb 20, 2014 at 10:01 PM

Wooww! @robertbu I owe you a couple brews man, not only did it work but that was really awesome of you . I really appreciate it along with everyones help. I will certainly pay it forward as I come up to speed. Fortunately, I also figured out to add that translation piece along the y.

One last question. Don't know if anyone here has matlab/Simulink experience and linking with Unity3D. Sounds farfetched, but there's an experiement we're trying to do in linking up Matlab/Simulink output data to drive let's say a simulation like the one I proposed but real-time. Don't know if you guys have seen somet$$anonymous$$ng like t$$anonymous$$s or know someone who has toyed around with t$$anonymous$$s concept.

using UnityEngine; using System.Collections;

public class RockTheBoat : MonoBehaviour {

 private AnimationCurve ac = new AnimationCurve();
 private AnimationCurve Fac = new AnimationCurve();
 private float maxTime;

 void Start() {
    LoadData("e:/data.csv");
    StartCoroutine(DoTheRocking(true));
 }

 private void LoadData(string filePath) {

    string input = System.IO.File.ReadAllText (filePath);
    string[] lines = input.Split (new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);

    for (int i = 0; i < lines.Length; i++) {
      string[] nums = lines[i].Split(new[] { ',' });
      if (nums.Length < 2) {
       Debug.Log ("Misforned input on line "+i+1);
      }

float timestamp; float anglex; float boby;

      if (float.TryParse (nums[0], out timestamp)) {    //Go to Colum1, Skip Row1-Start W/Row 2 and apply CSV data per iteration to Anim Key Fram Curves-Time Step        
       if (float.TryParse (nums[1], out anglex)) {  //Go to Colum2, Skip Row1-Start W/Row 2  and apply CSV data per iteration to Anim. curve (ac)-X-Angle Transform
              ac.AddKey (timestamp, anglex);
          if (float.TryParse (nums[2], out boby)) {  //Go to Colum3, Skip Row1-Start W/Row 2  and apply CSV data per iteration to Anim. curve (Fac) -Y-Tranlation Transform
                 Fac.AddKey (timestamp, boby);
             if (timestamp > maxTime)
                  maxTime = timestamp;
                 }        
                     
          }
             
      }
    }
 }

 private IEnumerator DoTheRocking(bool repeat) {
    do {
      float time = 0.0f;
      w$$anonymous$$le (time <= maxTime) {
       transform.eulerAngles = new Vector3(ac.Evaluate (time), 0.0f, 0.0f); //Angular Rotate About X-Axis (List)
       transform.position= new Vector3 (0.0f,Fac.Evaluate (time),0.0f); //Vertical Translate Along Y-Axis (Heave)
       yield return null;
       time += Time.deltaTime;
      }
    } w$$anonymous$$le (repeat);
  
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 robertbu · Feb 20, 2014 at 10:04 PM 0
Share
avatar image myozinaung · Sep 28, 2019 at 06:21 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Parenting Preventing Player Rotation 0 Answers

Multiple Cars not working 1 Answer

C# Need Help converting from Rigidbody to Character Controller 0 Answers

C# DragRigidbody ArgumentException: get_main can only be called from the main thread 2 Answers

C# Rotate More than Two GameObjects 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