我正在尝试重新创建“堆叠”游戏,在该游戏中,您将正方形切片彼此堆叠,并使其越来越高。应该发生的事情是,当单击屏幕时,方形块应停在其当前位置,然后在其顶部产生一个新的方形块。像这样:
但是,这就是游戏对我的影响。
生成方块并放置它。但是,下一个正方形棋子在与前一个棋子相同的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);
}
}
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
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
}
}
答案 0 :(得分:1)
我看不到任何代码更改停止后的最后一个多维数据集,因此您可以添加它。
internal void Stop()
{
...
MovingCube.LastCube = MovingCube.CurrentCube;
}