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 :expressionless: . 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. This 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. Think of something 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 within 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 this 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);

}

}

[22343-oscilating+rotation.png|22343]

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.

+1 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 things 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 this rotation off to use its own game object or combine that code into this 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', '

’ }, System.StringSplitOptions.RemoveEmptyEntries);

		for (int i = 0; i < lines.Length; i++) {
			string[] nums = lines*.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;*
    
  •  	while (time <= maxTime) {*
    
  •  		transform.eulerAngles = new Vector3(ac.Evaluate (time), 0.0f, 0.0f);*
    
  •  		yield return null;*
    
  •  		time += Time.deltaTime;*
    
  •  	}*
    
  •  } while (repeat);*
    
  • }*
    }

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 something like this or know someone who has toyed around with this 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', '

’ }, System.StringSplitOptions.RemoveEmptyEntries);

   for (int i = 0; i < lines.Length; i++) {
     string[] nums = lines*.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;
while (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;
}
} while (repeat);