我在使用 Unity 和 Vuforia 时遇到对象旋转问题。我有一个运动链(它是一个机械臂),我想控制他的运动。我有一个遥控器,它为机器人的每个链接发送一个带有角度位置的字符串。我解码字符串,然后为每个链接分配最终位置。我正在使用 slerp 来修改本地旋转。 当我在没有 Vuforia 的情况下尝试此代码时,代码运行良好并且链接会旋转,但是当我使用 Vuforia 并且链条位于标记上方时,我可以使链接旋转但随后所有链接都会回到其原始位置。我不知道为什么我有这种行为。
这是我正在使用的代码,我也尝试过更新每个链接的位置,但我想我不需要它,因为我正在使用局部角度。我正在通过 TCP/IP 接收数据。
using UnityEngine;
using Vuforia;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using TMPro;
public class MyListener : MonoBehaviour
{
public int connectionPort = 25001;
TcpListener listener;
string[] stringArray;
float[] Objetivo;
float[] posInicial;
float[] currAngle;
string msg;
public Transform[] Link = new Transform[6];
Quaternion[] startRotation = new Quaternion[6];
Quaternion[] currentRotation = new Quaternion[6];
Quaternion[] smoothRotation = new Quaternion[6];
Quaternion[] endRotation = new Quaternion[6];
int flagGiro, remoto;
public int flag;
float duration = 3f;
public TMP_Text Text1;
public TMP_Text Text2;
public TMP_Text Text3;
public TMP_Text Text4;
public TMP_Text Text5;
public TMP_Text Text6;
void Start()
{
flagGiro = 0;
flag=0;
remoto = 0;
listener = new TcpListener(IPAddress.Any, connectionPort);
listener.Start();
for (int i=0;i<6;i++){
startRotation[i] = Quaternion.Euler(0, 0, 0);
}
}
void Update()
{
if (!listener.Pending()){
flag=0;
for (int i = 0; i < 6; i++)
{
Link[i].localRotation = endRotation[i];
}
if (remoto==1)
{
for (int i = 0; i < 6; i++)
{
Link[i].localRotation = endRotation[i];
}
}
}
else {
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();
StreamReader reader = new StreamReader(ns);
msg = reader.ReadToEnd();
stringArray = ParseData(msg);
Text1.text = stringArray[0];
Text2.text = stringArray[1];
Text3.text = stringArray[2];
Text4.text = stringArray[3];
Text5.text = stringArray[4];
Text6.text = stringArray[5];
//x,y,z también pueden ser un array
float y = float.Parse(stringArray[0]);
float x = float.Parse(stringArray[1]);
float x1 = float.Parse(stringArray[2]);
float y1 = float.Parse(stringArray[3]);
float z = float.Parse(stringArray[4]);
float x3 = float.Parse(stringArray[5]);
endRotation[0] = Quaternion.Euler(Link[0].localEulerAngles.x, y, Link[0].localEulerAngles.z); //endRotation[0] = Quaternion.Euler(0, y/div, 0);
endRotation[1] = Quaternion.Euler(x, Link[1].localEulerAngles.y, Link[1].localEulerAngles.z);
endRotation[2] = Quaternion.Euler(x1, Link[2].localEulerAngles.y, Link[2].localEulerAngles.z);
endRotation[3] = Quaternion.Euler(Link[3].localEulerAngles.x, y1, Link[3].localEulerAngles.z);
endRotation[4] = Quaternion.Euler(Link[4].localEulerAngles.x, Link[4].localEulerAngles.y, z);
endRotation[5] = Quaternion.Euler(x3, Link[5].localEulerAngles.y, Link[5].localEulerAngles.z);
// Obtener las rotaciones iniciales de los objetos secundarios
for (int i = 0; i < 6; i++)
{
startRotation[i] = Link[i].localRotation;
}
StartCoroutine(RotateObject(duration));
}
}
public static string[] ParseData(string dataString)
{
Debug.Log(dataString);
if (dataString[0]==':'){
dataString = dataString.Remove(dataString.Length-4);
dataString = dataString.Substring(1);
}
// Split the elements into an array
string[] stringArray = dataString.Split('/');
return stringArray;
}
IEnumerator RotateObject(float duration)
{
for (int i = 0; i < 6; i++)
{
startRotation[i] = Link[i].localRotation;
}
float elapsedTime = 0f;
while (elapsedTime < duration)
{
for (int i = 0; i < 6; i++)
{
smoothRotation[i] = Quaternion.Slerp(startRotation[i], endRotation[i], elapsedTime / duration);
Link[i].localRotation = smoothRotation[i];
}
elapsedTime += Time.deltaTime;
yield return null;
}
for (int i = 0; i < 6; i++)
{
Link[i].localRotation = endRotation[i];
Vector3 endPosition = Link[i].localPosition;
Link[i].localPosition = endPosition;
}
remoto = 1;
}
}
感谢您的合作。谢谢