使用自己的坐标转换Unity中子对象的位置

时间:2017-05-23 16:51:23

标签: c# opencv unity3d

我希望标题不会太混乱。目前,我将空白游戏对象的位置转换为使用它进行LookAt旋转。问题是,我对子对象做错了。我将简单地发布层次结构的图片。 Hierachy of LookAt Object

我想用我的LookAtLeft和LookAtRight坐标转换位置,让两个肩膀用LookAt看它。遗憾的是,这不适用于我目前的脚本。两个LookAt对象与我的坐标不在同一个x和y位置。

这是我使用的脚本:

using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

public class SocketCLient : MonoBehaviour {

    // Use this for initialization

    public GameObject lefthand;
    public GameObject righthand;
    public GameObject body;
    public float xPos = 10.0f;
    public float yPos = 10.0f;
    public float ryPos = 10.0f;
    public float rxPos = 10.00f;
    private float bxPos = 0;
    private float byPos = 0;
    private String newString;

    Thread receiveThread;
    UdpClient client;
    public int port;

    //info

    public string lastReceivedUDPPacket = "";
    public string allReceivedUDPPackets = "";

    void Start () {
        init();
    }

    void OnGUI(){
        Rect  rectObj=new Rect (40,10,200,400);

        GUIStyle  style  = new GUIStyle ();

        style .alignment  = TextAnchor.UpperLeft;

        GUI .Box (rectObj,"# UDPReceive\n127.0.0.1 "+port +" #\n"

                  //+ "shell> nc -u 127.0.0.1 : "+port +" \n"

                  + "\nLast Packet: \n"+ lastReceivedUDPPacket

                  // + "\n\nAll Messages: \n"+allReceivedUDPPackets

                  ,style );

    }

    private void init(){
        print ("UPDSend.init()");

        port = 5065;

        print ("Sending to 127.0.0.1 : " + port);

        receiveThread = new Thread (new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start ();

    }

    private void ReceiveData(){
        client = new UdpClient (port);
        while (true) {
            try{
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
                byte[] data = client.Receive(ref anyIP);
                string[] separators = {",", ".", "!", "?", ";", ":", " "};
                string text = Encoding.UTF8.GetString(data);
                // print (">> " + text);
                lastReceivedUDPPacket=text;
                allReceivedUDPPackets=allReceivedUDPPackets+text;
                if (text.Contains("leftx")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    newString = text.Substring(5, 3);
                    xPos = int.Parse(words[1]);
                    print(xPos);
                }
                if (text.Contains("lefty")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    yPos = int.Parse(words[1]);
                    newString = text.Substring(5, 3);
                    // yPos = int.Parse(newString);
                    // print("yPos: ");
                    // print(yPos);
                    // if (yPos > 140)
                    //  yPos = -yPos;
                }
                if (text.Contains("bodyx")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    bxPos = int.Parse(words[1]);
                    newString = text.Substring(5, 3);
                    // bxPos = int.Parse(newString);
                }
                if (text.Contains("bodyy")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    byPos = int.Parse(words[1]);
                    newString = text.Substring(5, 3);
                    // byPos = int.Parse(newString);
                }
                if (text.Contains("righty")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    ryPos = int.Parse(words[1]);
                    newString = text.Substring(6, 3);
                    // ryPos = int.Parse(newString);
                }
                if (text.Contains("rightx")){
                    string[] words = text.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                    rxPos = int.Parse(words[1]);
                    newString = text.Substring(6, 3);
                    // rxPos = int.Parse(newString);

                }
                //xPos = int.Parse(text);
                //xPos *= 0.021818f;
            }catch(Exception e){
                // print (e.ToString());
            }
        }
    }

    public string getLatestUDPPacket(){
        allReceivedUDPPackets = "";
        return lastReceivedUDPPacket;
    }

    // Update is called once per frame
    void Update () {

        // print("xPos " + xPos.ToString());
        // print("rxPos " + rxPos.ToString());
        body.transform.position = new Vector3(bxPos, byPos);
        lefthand.transform.position = new Vector3(xPos, -yPos);
        righthand.transform.position = new Vector3(-rxPos, -ryPos);
        print("rxPos: " + rxPos.ToString() + " ryPos: " + ryPos.ToString());
        // if(yPos < byPos)
        //  lefthand.transform.position = new Vector3(xPos, yPos , 0);
        // if(yPos > byPos)
        //  lefthand.transform.position = new Vector3(xPos, yPos , 0);  
        //lefthand.transform.Rotate(0, yPos,  -10, Space.World);
    }

    void OnApplicationQuit(){
        if (receiveThread != null) {
            receiveThread.Abort();
            Debug.Log(receiveThread.IsAlive); //must be false
        }
    }
}

0 个答案:

没有答案