Moving a instantiated gameobject

馋奶兔 提交于 2020-01-17 05:03:15

问题


I want to change the position of a instantiated game object. For that I have used a UI button when the user clicks on the button a cube will be instantiated and when user clicks on that instantiated cube and move a UI slider, the position of that cube will be changed according to the value given by the slider.

I tried this way but it doesn't work. What am I doing wrong here

using UnityEngine;
using System.Collections;

public class instantiate : MonoBehaviour
{
    public GameObject cube;
    public float speed = 0f;
    public float pos = 0f;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                Debug.Log("Clicked");

                if (hit.collider.tag == "Cube")
                {

                    // Destroy(hit.collider.gameObject);

                    // Destroy(this);
                    speed += Input.GetAxis("Horizontal");
                    hit.collider.gameObject.transform.eulerAngles = new Vector3(0, 0, speed);
                    hit.collider.gameObject.transform.position = new Vector3(0, 0, pos);//pos
                }
            }
        }

    }

    public void objinst()
    {
        Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);
    }

    public void rotatess(float newspeed)
    {
        speed = newspeed;

    }

    public void positions(float newpos)
    {

        pos = newpos;
    }
}

回答1:


You are supposed to have a callback function that gets called when the Button is clicked and another one that gets called when the Slider value changes. I can't tell if you are doing this from the Editor but the way your functions are named, we can't tell which is one being called during Button click or Slider value change...

Put your Instantiate code in your Button callback function then put your cube moving code in the Slider value change callback function.

In your Raycast code that detects the cube click, store the Transform reference of the cube to a global Transform variable. This stored Transform is what you will use to move the cube in your Slider value change callback function.

You subscribe to Button click event with Button.onClick.AddListener(instantiateButtonCallBackFunction); then to Slider value change event with Slider.onValueChanged.AddListener(delegate { sliderCallBackFunction(cubeSlider.value); });

Here is what it should look like. Everything is done with code. Simply drag the Cube prefab, Slider and Button to the right slot and it should work. When a Button is clicked, Cube is instantiated. When the cube is clicked, you will be able to move it with the slider.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class instantiate : MonoBehaviour
{
    public GameObject cubePrefab;
    public Slider cubeSlider;
    public Button instantiateButton;

    public float speed = 0f;
    public float pos = 0f;


    private Transform currentObjectToDrag = null;

    // Use this for initialization
    void Start()
    {
        //Set Slider Values
        cubeSlider.minValue = 0f;
        cubeSlider.maxValue = 50f;
        cubeSlider.value = 0f;

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 1000.0f))
            {
                GameObject objHit = hit.collider.gameObject;
                Debug.Log("We Clicked on : " + objHit.name);

                //Check if this is cube
                if (objHit.CompareTag("Cube"))
                {
                    Debug.Log("Cube selected. You can now drag the Cube with the Slider!");
                    //Change the current GameObject to drag
                    currentObjectToDrag = objHit.transform;
                }
            }
        }
    }

    public void instantiateCube()
    {
        //Instantiate(cubePrefab, new Vector3(0, 0, 0), Quaternion.identity);
        Instantiate(cubePrefab, new Vector3(-15.1281f, 0.67f, 7.978208f), Quaternion.identity);
    }

    public void rotatess(float newspeed)
    {
        speed = newspeed;

    }

    public void positions(float newpos)
    {
        pos = newpos;
    }

    //Called when Instantiate Button is clicked
    void instantiateButtonCallBack()
    {
        Debug.Log("Instantiate Button Clicked!");
        instantiateCube();
    }

    //Called when Slider value changes
    void sliderCallBack(float value)
    {
        Debug.Log("Slider Value Moved : " + value);

        //Move the Selected GameObject in the Z axis with value from Slider
        if (currentObjectToDrag != null)
        {
            currentObjectToDrag.position = new Vector3(0, 0, value);
            Debug.Log("Position changed!");
        }
    }

    //Subscribe to Button and Slider events
    void OnEnable()
    {
        instantiateButton.onClick.AddListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.AddListener(delegate { sliderCallBack(cubeSlider.value); });
    }

    //Un-Subscribe to Button and Slider events
    void OnDisable()
    {
        instantiateButton.onClick.RemoveListener(instantiateButtonCallBack);
        cubeSlider.onValueChanged.RemoveListener(delegate { sliderCallBack(cubeSlider.value); });
    }
}



回答2:


You need to store a reference to the instance you've created.

GameObject myCube = Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);

You can then control the position of it using that reference.

myCube.transform.position.x = 10;


来源:https://stackoverflow.com/questions/39156770/moving-a-instantiated-gameobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!