我想利用unity脚本控制物体旋转,三维方向的旋转角度分别是txt文件中的前三组数,请问是哪里出问题了,应该怎么写?
问题描述:
我想利用unity脚本控制物体旋转,三维方向的旋转角度分别是txt文件中的前三组数,请问是哪里出问题了,应该怎么写?
附图1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class wristA : MonoBehaviour
{
private void RotateDynamic(float a, float b, float c);
private string[] strArray;// Use this for initialization
void Start()
{
StreamReader sr = new StreamReader(@"C:\\Users\\14586\\Desktop\\data1.txt");
string[] line1 = sr.ReadLine().Split(',');
a = double.Parse(line1[1]);
b = double.Parse(line1[2]);
c = double.Parse(line1[3]);
}
// Update is called once per frame
void Update()
{
transform.Rotate(0, a, 0, Space.Self);
transform.Rotate(b, 0, 0, Space.Self);
transform.Rotate(0, 0, c, Space.Self);
}
}
答
你这是写js和python写习惯了是吗,你这a,b,c都没定义,能编译过去就出鬼了
还有,你文件打开了就不关闭,使用using块包住streamreader让它能自动释放资源
还有,你字符串已经加了@符号,怎么还写\,这路径能对吗
错误一大堆
答
```c#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class wristC : MonoBehaviour
{
public GameObject go;//变量要放在类里面,你定义的go也没用上,我估计是你想旋转的物体
float a, b, c;//要使用全局变量要一开始定义好。另外应为float类型而非int类型
//方法要用大括号而非分号,另外这个方法你好像也没用到,没用的代码可以删除;
private void RotateDynamic(float a, float b, float c)
{
}
private string[] strArray;// 没用到。可以删除
void Start()
{
StreamReader sr = new StreamReader(@"C:\Users\57622\Desktop\data1.txt");
string[] line1 = sr.ReadLine().Split(',');
//类型错误
a = float.Parse(line1[1]);
b = float.Parse(line1[2]);
c = float.Parse(line1[3]);
Debug.Log(a+"::"+b+"::"+c);
}
// Update is called once per frame
void Update()
{
// 这里就不是用transform了,transform挂在哪个物体上哪个物体就旋转
go.transform.Rotate(new Vector3(a,b,c), Space.Self);
}
}
```
答
你是有报错没解决啊