Question by
Oyunja · Oct 27, 2021 at 09:08 AM ·
rotation axis
How to change position to rotation object?
I'm a beginner at Unity. I want to rotate my cube by given angle data. This is referenced code. I changed position to rotation. But not worked. Help me???
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CSV_1 : MonoBehaviour
{
private struct Step
{
public float speed;
public Vector3 rotation;
}
public Transform Cube;
public TextAsset CsvFile;
private List<Step> steps;
private int currentStep = 0;
// Start is called before the first frame update
void Start()
{
ReadCSV();
Cube.rotation = steps[0].rotation;
}
// Update is called once per frame
void Update()
{
if ((Cube.rotation - steps[currentStep].rotation).sqrMagnitude < 0.1f)
currentStep = (currentStep + 1) % steps.Count;
Cube.rotation = Vector3.MoveTowards(Cube.rotation, steps[currentStep].rotation, steps[currentStep].speed * Time.deltaTime);
}
void ReadCSV()
{
steps = new List<Step>();
string[] records = CsvFile.text.Split('\n');
for (int i= 1; i <records.Length; i++)
{
string[] array = records[i].Split(',');
steps.Add(
new Step()
{
speed = float.Parse(array[0]),
rotation = new Vector3(float.Parse(array[1]), float.Parse(array[2]), float.Parse(array[3]))
}
);
}
}
}
Comment