Unity中location可以和Vector3向量相加减和 通过new Vector3更改位置。如:
public Vector3 cubeposition = new Vector3(0,0,0);
cubeposition = new Vector3(Xadjustposition, Yadjustposition, Zadjustposition);
GameObject.Find("newcube").transform.position = cubeposition;
但rotation不行,要用Quaternion.Euler赋值的办法才能赋值或更改角度。如:
public Quaternion cuberotation = Quaternion.identity;//四元素初始化只能这样
cuberotation = Quaternion.Euler(Xadjustrotation, Yadjustrotation, Zadjustrotation);
GameObject.Find("newcube").transform.localRotation=cuberotation;
或者GameObject.Find("newcube").transform.localRotation=Quaternion.Euler(Xadjustrotation, Yadjustrotation, Zadjustrotation);
综合:
GameObject.Find("newcube").transform.localPosition = cuberotation * new Vector3(translation.y, translation.z, -translation.x) + cubeposition;
下面是更完整一点儿的摘抄:
float Xadjustposition, Yadjustposition, Zadjustposition, Xadjustrotation, Yadjustrotation, Zadjustrotation;
public Vector3 cubeposition = new Vector3(0,0,0);
public Quaternion cuberotation = Quaternion.identity;//四元素初始化只能这样
Matrix4x4 matrix = new Matrix4x4();
matrix.SetRow(0, new Vector4(m[3], m[6], m[9], m[12] / scaleMultiplier));//经过实际测试前三个都是0,从第四个开始才跟transform中的数据一样,跟openIGTLink官方的协议说明不同,按实际的来,所以从m[3]开始
matrix.SetRow(1, new Vector4(m[4], m[7], m[10], m[13] / scaleMultiplier));
matrix.SetRow(2, new Vector4(m[5], m[8], m[11], m[14] / scaleMultiplier));
matrix.SetRow(3, new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
Vector3 eulerAngles = matrix.rotation.eulerAngles;
GameObject.Find("newcube").transform.localRotation = Quaternion.Euler(eulerAngles.x, -eulerAngles.y, -eulerAngles.z);
Vector3 markerposition = GameObject.Find("ImageTarget2").transform.position;//ImageTarget2
Quaternion markerrotation = GameObject.Find("ImageTarget2").transform.rotation;
cubeposition = markerposition + markerrotation * new Vector3(0, -0.03f, -0.114f);
cubeposition = markerposition + markerrotation * (cubeposition - markerposition);//此句虽然是用的向量加旋转,但依然是眼镜获得的两个全局值再运算,跟上面一个效果,多一层误差。
public void XpositionPlus()
{
float result = 0;
string textValue = XpositionInput.GetComponent<InputField>().text;
result = float.Parse(textValue);
Xadjustposition = Xadjustposition + result / 1000;
cubeposition = new Vector3(Xadjustposition, Yadjustposition, Zadjustposition);
GameObject.Find("newcube").transform.position = cubeposition;
}
//以下是rotation
public void XrotationPlus()
{
float result = 0;
string textValue = XrotationInput.GetComponent<InputField>().text;
result = float.Parse(textValue);
Xadjustrotation = Xadjustrotation + result;
cuberotation = Quaternion.Euler(Xadjustrotation, Yadjustrotation, Zadjustrotation);文章来源:https://www.toymoban.com/news/detail-401664.html
GameObject.Find("newcube").transform.localRotation=cuberotation;
}文章来源地址https://www.toymoban.com/news/detail-401664.html
到了这里,关于Unity中location和rotation赋值和更改的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!