简体   繁体   中英

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

I am trying to select a single/multiple objects and translating them using x,y,z coordinates panel. What I have done already is moving them to a specific coordinate, but not translating. In-order to translate them, I must get the current xyz coordinates of the selected objects and add to them the xyz coordinates which the user have written into the panel. Can anyone help me with this issue?

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 contains the current position. so your update function would look like

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);
}

By the way, you don't need to Update the position all the time, you can use the events from the InputFields. Make a function like

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

and assign it in Unity:

在此处输入图像描述

if you just want to add the vector instead of setting it then simply replace

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

by

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

or alternatively if it is easier to read for you use according method Transform.Translate

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

If you rather want hem all to move independently in their according local space then rather use

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

or accordingly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM