我正在嘗試選擇單個/多個物件并使用 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();
}
}
uj5u.com熱心網友回復:
如果您只想添加向量而不是設定它,那么只需替換
target.transform.position = new Vector3(xPos, yPos, zPos);
經過
// |
// V
target.transform.position = new Vector3(xPos, yPos, zPos);
或者,如果您更容易閱讀,請使用相應的方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461621.html
下一篇:返回列表