简体   繁体   English

Unity堆栈游戏c#

[英]Unity stack game c#

I am trying to recreate the Stack game where you stack square slices on top of each other and get it higher and higher.我正在尝试重新创建堆栈游戏,您可以在其中将方形切片堆叠在一起并使其越来越高。 What's supposed to happen is the when the screen is clicked the square piece should stop at its current position and then a new square piece should spawn on top of it.应该发生的是当点击屏幕时,方块应该停在它的当前位置,然后一个新的方块应该在它上面产生。 Like so:像这样:

Optimal Output最佳输出

However, this is what the game is doing on my end.然而,这就是游戏正在做的事情。

Current Output电流输出

Current Output2电流输出2

The square piece spawns and is placeable.方块会生成并且是可放置的。 However, the next square piece spawns on the same Y-axis as the previous piece instead of spawning on top of the previous piece.但是,下一个方块生成在与前一个方块相同的 Y 轴上,而不是生成在前一个方块的顶部。 From there it overlaps the previous pieces.从那里它与之前的部分重叠。 Heres the scripts that have been written so far.下面是到目前为止编写的脚本。 Does anyone have an idea of how to fix this?有谁知道如何解决这个问题? Specifically how to get the script to get the optimal output as pictured above?具体如何让脚本获得如上图所示的最佳输出?

CubeSpawner.cs 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);
    }
}

GameManager.cs游戏管理器

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

MovingCube.cs移动立方体.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
    }
}

I don't see any code changes the last cube after stop, so you may add it.我没有看到任何代码在停止后更改最后一个多维数据集,因此您可以添加它。

internal void Stop()
{
    ...

    MovingCube.LastCube = MovingCube.CurrentCube;
}

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

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