繁体   English   中英

Unity堆栈游戏c#

[英]Unity stack game c#

我正在尝试重新创建堆栈游戏,您可以在其中将方形切片堆叠在一起并使其越来越高。 应该发生的是当点击屏幕时,方块应该停在它的当前位置,然后一个新的方块应该在它上面产生。 像这样:

最佳输出

然而,这就是游戏正在做的事情。

电流输出

电流输出2

方块会生成并且是可放置的。 但是,下一个方块生成在与前一个方块相同的 Y 轴上,而不是生成在前一个方块的顶部。 从那里它与之前的部分重叠。 下面是到目前为止编写的脚本。 有谁知道如何解决这个问题? 具体如何让脚本获得如上图所示的最佳输出?

CubeSpawner.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CubeSpawner : MonoBehaviour
{
    [SerializeField]     
    private MovingCube cubePrefab;

    public void SpawnCube()
    {
        var cube = Instantiate(cubePrefab);

        if (MovingCube.LastCube != null && MovingCube.LastCube.gameObject != GameObject.Find("Start"))
        {
        cube.transform.position = new Vector3(transform.position.x,
            MovingCube.LastCube.transform.position.y + cubePrefab.transform.localScale.y,
            transform.position.z);
        }
        else
        {
            cube.transform.position = transform.position;
        }
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(transform.position, cubePrefab.transform.localScale);
    }
}

游戏管理器

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour

{
    private void Update()
    {
        if (Input.GetButtonDown("Fire1")) //if play presses left-click, control, tap a screen etc
        {
            if (MovingCube.CurrentCube != null)
                MovingCube.CurrentCube.Stop();

            FindObjectOfType<CubeSpawner>().SpawnCube();
        }
    }
}

移动立方体.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingCube : MonoBehaviour
{
    public static MovingCube CurrentCube { get; private set; }
    public static MovingCube LastCube { get; private set; }

    [SerializeField] //allows variable to be adjustable during runtime
    private float MoveSpeed = 1f;

    private void OnEnable()
    {
        if (LastCube == null)
            LastCube = GameObject.Find("Start").GetComponent<MovingCube>(); 

        CurrentCube = this;
        GetComponent<Renderer>().material.color = GetComponentRandomColor();

        transform.localScale = new Vector3(LastCube.transform.localScale.x, transform.localScale.y, LastCube.transform.localScale.z);

    }

    private Color GetComponentRandomColor()
    {
        return new Color(UnityEngine.Random.Range(0, 1f), UnityEngine.Random.Range(0, 1f), UnityEngine.Random.Range(0, 1f));
    }

    internal void Stop()
    {
        MoveSpeed = 0;
        float hangover = transform.position.z - LastCube.transform.position.z;

        float direction = hangover > 0 ? 1f : -1f;
        SplitCubeOnZ(hangover, direction);
    }

    private void SplitCubeOnZ(float hangover, float direction)
    { 
        float newZSize = LastCube.transform.localScale.z - Mathf.Abs(hangover);
        float fallingBlockSize = transform.localScale.z - newZSize;

        float newZPosition = LastCube.transform.position.z + (hangover / 2);
        transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, newZSize);
        transform.position = new Vector3(transform.position.x, transform.position.y, newZPosition);

        float cubeEdge = transform.position.z + (newZSize / 2f * direction);
        float fallingBlockZPosition = cubeEdge + fallingBlockSize / 2f * direction;

        SpawnDropCube(fallingBlockZPosition, fallingBlockSize);
    }

    private void SpawnDropCube(float fallingBlockZPosition, float fallingBlockSize)
    {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, fallingBlockSize);
        cube.transform.position = new Vector3(transform.position.x, transform.position.y, fallingBlockZPosition);

        cube.AddComponent<Rigidbody>();
        cube.GetComponent<Renderer>().material.color = GetComponent<Renderer>().material.color;
        Destroy(cube.gameObject, 1f);
    }

    private void Update()
    {
        transform.position += transform.forward * Time.deltaTime * MoveSpeed; //moves the square piece
    }
}

我没有看到任何代码在停止后更改最后一个多维数据集,因此您可以添加它。

internal void Stop()
{
    ...

    MovingCube.LastCube = MovingCube.CurrentCube;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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