簡體   English   中英

選擇單個/多個對象並使用 Unity 中的坐標移動它們

[英]Selecting a single/multiple Objects and moving them using coordinates in Unity

我正在嘗試 select 單個/多個對象並使用 x、y、z 坐標面板轉換它們。 我已經做的是將它們移動到一個特定的坐標,而不是平移。 為了翻譯它們,我必須獲取所選對象的當前 xyz 坐標,並將用戶寫入面板的 xyz 坐標添加到它們中。 誰能幫我解決這個問題?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using TMPro;

public class InstantiateWithButton : MonoBehaviour
{

public GameObject[] prefabs = null;
public Camera cam = null;

public GameObject XYZPanel;

// public Vector3 newposition;

public float xPos;
public float yPos;
public float zPos;

public TMP_InputField inputx;
public TMP_InputField inputy;
public TMP_InputField inputz;

public GameObject target;

public List<GameObject> targets = new List<GameObject> ();


void Start()
{
    XYZPanel.SetActive(false);
    inputx.text = 0.0f.ToString();
    inputy.text = 0.0f.ToString();
    inputz.text = 0.0f.ToString();

}

void Update()
{
    InstantiateObject();

    xPos = float.Parse(inputx.text);
    yPos = float.Parse(inputy.text);
    zPos = float.Parse(inputz.text);

}

public void InstantiateObject()
{
    if(!EventSystem.current.IsPointerOverGameObject())
    {
        if(Input.GetMouseButtonDown(2))
        {
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if(Physics.Raycast(ray, out hit))
        {
            if(hit.collider.gameObject.tag == "atom")
            {
                Instantiate(target, hit.point, Quaternion.identity);
            }
            if(hit.collider.gameObject.tag == "Object")
            {
                XYZPanel.SetActive(true);
                targets.Add(hit.collider.gameObject);
            }
        }

        }   
    }

}


public void moveObject()
{

    foreach(var target in targets)
    {
        target.transform.position = new Vector3(xPos, yPos, zPos);
    }
    targets.Clear();
}





}

transform.position包含當前的 position。因此您的更新 function 看起來像

void Update()
{
    InstantiateObject();

    xPos = transform.position.x + float.Parse(inputx.text);
    yPos = transform.position.y + float.Parse(inputy.text);
    zPos = transform.position.z + float.Parse(inputz.text);
}

順便說一下,您不需要一直更新 position,您可以使用 InputFields 中的事件。 制作一個 function 贊

public void PositionChanged(string newPos)
{
    xPos = transform.position.x + float.Parse(newPos);
}

並在 Unity 中分配它:

在此處輸入圖像描述

如果您只想添加矢量而不是設置它,則只需替換

target.transform.position = new Vector3(xPos, yPos, zPos);

經過

//                        |
//                        V
target.transform.position += new Vector3(xPos, yPos, zPos);

或者,如果根據方法Transform.Translate使用它更容易閱讀

target.transform.Translate(new Vector3(xPos, yPos, zPos), Space.World);

如果您更希望下擺全部在其相應的本地空間中獨立移動,那么請使用

target.transform.localPosition += new Vector3(xPos, yPos, zPos);

或相應地

target.transform.Translate(new Vector3(xPos, yPos, zPos));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM