Saya baru mengenal unit dan saya masih belum tahu banyak, dan saya masih mencoba mencari tahu dalam banyak hal, jadi tolong jangan bersumpah. Proyek pertama saya memutuskan untuk membuat Tetris dan saya punya masalah. Saya mencoba membuat kontrol angka melalui Tombol UI dan masalahnya adalah kontrol tidak berfungsi dengan klon yang saya buat melalui Instantiate, namun, dengan objek asli, jika saya hanya mentransfernya ke panggung, kontrol berfungsi diam-diam. Bagaimana cara membuat Tombol UI berfungsi sama untuk klon? Terima kasih sebelumnya atas balasan Anda.

using System.Collections.Generic;
using UnityEngine;


public class Group : MonoBehaviour
{
    float lastFall = 0;


    // Start is called before the first frame update
    void Start()
    {
        if (!isValidGridPos())
        {
            Debug.Log("GAME OVER");
            Destroy(gameObject);
        }      
    }

    // Update is called once per frame
    void Update()
    {
        //Move Left
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            transform.position += new Vector3(-1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(1, 0, 0);
            }
        }



        // Move Right
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.position += new Vector3(1, 0, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.position += new Vector3(-1, 0, 0);
            }
        }

        // Rotate
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            transform.Rotate(0, 0, -90);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            {
                transform.Rotate(0, 0, 90);
            }
        }

        //Down
        else if (Input.GetKeyDown(KeyCode.DownArrow) ||
                 Time.time - lastFall >= 1)
        {

            transform.position += new Vector3(0, -1, 0);

            if (isValidGridPos())
            {
                updateGrid();
            }
            else
            { 
                transform.position += new Vector3(0, 1, 0);
                Playfield.deleteFullRows();
                FindObjectOfType<Spawner>().SpawnNext();
                enabled = false;
            }
            lastFall = Time.time;
        }
    }


    bool isValidGridPos()
    {
        foreach (Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);

            if (!Playfield.insideBorder(v))
            {
                return false;
            }

            if (Playfield.grid[(int)v.x, (int)v.y] != null &&
                Playfield.grid[(int)v.x, (int)v.y].parent != transform)
            {
                return false;
            }
        }
        return true;
    }

    void updateGrid()
    {
        for (int y = 0; y < Playfield.h; ++y)
            for (int x = 0; x < Playfield.w; ++x)
                if (Playfield.grid[x, y] != null)
                    if (Playfield.grid[x, y].parent == transform)
                        Playfield.grid[x, y] = null;

        foreach (Transform child in transform)
        {
            Vector2 v = Playfield.roundVec2(child.position);
            Playfield.grid[(int)v.x, (int)v.y] = child;
        }
    }

    public void MoveLeft()
    {
        transform.position += new Vector3(-1, 0, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(1, 0, 0);
        }
    }

    public void MoveRight()
    {      
        transform.position += new Vector3(1, 0, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(-1, 0, 0);
        }
        
    }

    public void Rotate()
    {            
        transform.Rotate(0, 0, -90);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.Rotate(0, 0, 90);
        }
        
    }

    public void MoveDown()
    {
        transform.position += new Vector3(0, -1, 0);

        if (isValidGridPos())
        {
            updateGrid();
        }
        else
        {
            transform.position += new Vector3(0, 1, 0);
            Playfield.deleteFullRows();
            FindObjectOfType<Spawner>().SpawnNext();
            enabled = false;
        }

    }
}
0
sar marat 17 Mei 2021, 20:47

1 menjawab

Jawaban Terbaik

HI Saya memeriksa apa yang Anda katakan dan jika saya memahaminya, Anda kehilangan referensi ke objek baru dan tidak dapat memindahkannya. Saya membuat kode sampel, Anda dapat mencoba dan memeriksanya

public Transform currentObject;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if (currentObject != null)
    {
        if (Input.GetKey(KeyCode.A))
        {
            currentObject.position += new Vector3(-1, 0, 0)*Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.D))
        {
            currentObject.position += new Vector3(1, 0, 0) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.W))
        {
            currentObject.position += new Vector3(0, 1, 0) * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.S))
        {
            currentObject.position += new Vector3(0, -1, 0) * Time.deltaTime;
        }
    }
}
public void SetCurrentObj(Transform obj){     currentObject = obj;}

//untuk membuat kubus setiap 10 detik dan Anda dapat mengontrolnya

public GameObject prefab;
public float tik;
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if(Time.time>tik)
    {
        GameObject temp = Instantiate(prefab,transform.position,Quaternion.identity);
        FindObjectOfType<Controller>().SetCurrentObj(temp.transform);
        tik = Time.time + 10;
    }
}
1
Siavosh inanlou 17 Mei 2021, 19:31