[英]Unity stack game c#
我正在嘗試重新創建堆棧游戲,您可以在其中將方形切片堆疊在一起並使其越來越高。 應該發生的是當點擊屏幕時,方塊應該停在它的當前位置,然后一個新的方塊應該在它上面產生。 像這樣:
然而,這就是游戲正在做的事情。
方塊會生成並且是可放置的。 但是,下一個方塊生成在與前一個方塊相同的 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.