统一堆栈游戏c#
我正在尝试重新创建一个堆叠游戏,在这个游戏中,你将正方形的薄片堆叠在彼此的顶部,并使其越来越高。应该发生的是,当屏幕被点击时,方块应该停止在其当前位置,然后一个新的方块应该在其顶部生成。像这样: 然而,这就是我这边的比赛 方形块生成并可放置。但是,下一个方形工件与上一个工件在相同的Y轴上繁殖,而不是在上一个工件的顶部繁殖。从那里它与前面的部分重叠。以下是迄今为止已经编写的脚本。有人知道如何解决这个问题吗?具体来说,如何让脚本获得上图所示的最佳输出 cubespowner.cs统一堆栈游戏c#,c#,unity3d,C#,Unity3d,我正在尝试重新创建一个堆叠游戏,在这个游戏中,你将正方形的薄片堆叠在彼此的顶部,并使其越来越高。应该发生的是,当屏幕被点击时,方块应该停止在其当前位置,然后一个新的方块应该在其顶部生成。像这样: 然而,这就是我这边的比赛 方形块生成并可放置。但是,下一个方形工件与上一个工件在相同的Y轴上繁殖,而不是在上一个工件的顶部繁殖。从那里它与前面的部分重叠。以下是迄今为止已经编写的脚本。有人知道如何解决这个问题吗?具体来说,如何让脚本获得上图所示的最佳输出 cubespowner.cs using
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();
}
}
}
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
}
}
GameManager.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();
}
}
}
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
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类GameManager:MonoBehavior
{
私有void更新()
{
if(Input.GetButtonDown(“Fire1”)//如果播放按左键,则控制、点击屏幕等
{
if(MovingCube.CurrentCube!=null)
MovingCube.CurrentCube.Stop();
FindObjectOfType().SpawnCube();
}
}
}
MovingCube.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();
}
}
}
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
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类MovingCube:单行为
{
public static MovingCube CurrentCube{get;private set;}
public static MovingCube LastCube{get;private set;}
[SerializeField]//允许在运行时调整变量
专用浮球移动速度=1f;
私有void OnEnable()
{
if(LastCube==null)
LastCube=GameObject.Find(“Start”).GetComponent();
CurrentCube=这个;
GetComponent().material.color=GetComponentRandomColor();
transform.localScale=newvector3(LastCube.transform.localScale.x、transform.localScale.y、LastCube.transform.localScale.z);
}
专用颜色GetComponentRandomColor()
{
返回新颜色(UnityEngine.Random.Range(0,1f)、UnityEngine.Random.Range(0,1f)、UnityEngine.Random.Range(0,1f));
}
内部空隙停止()
{
移动速度=0;
float hangover=transform.position.z-LastCube.transform.position.z;
浮动方向=残留>0?1f:-1f;
SplitCubeOnZ(宿醉,方向);
}
私有空间分割立方体(浮动遗留,浮动方向)
{
float newZSize=LastCube.transform.localScale.z-Mathf.Abs(遗留);
float-fallingBlockSize=transform.localScale.z-newZSize;
float newZPosition=LastCube.transform.position.z+(hangover/2);
transform.localScale=newvector3(transform.localScale.x、transform.localScale.y、newZSize);
transform.position=newvector3(transform.position.x,transform.position.y,newZPosition);
float cubeEdge=transform.position.z+(newZSize/2f*方向);
浮动落块ZPosition=cubeEdge+fallingBlockSize/2f*方向;
生成DropCube(fallingBlockZPosition,fallingBlockSize);
}
私有DropCube(浮点fallingBlockZPosition,浮点fallingBlockSize)
{
var cube=GameObject.CreatePrimitive(PrimitiveType.cube);
cube.transform.localScale=newvector3(transform.localScale.x,transform.localScale.y,fallingBlockSize);
cube.transform.position=新矢量3(transform.position.x,transform.position.y,fallingBlockZPosition);
AddComponent();
cube.GetComponent().material.color=GetComponent().material.color;
销毁(cube.gameObject,1f);
}
私有void更新()
{
transform.position+=transform.forward*Time.deltaTime*MoveSpeed;//移动正方形
}
}
我没有看到任何代码更改停止后的最后一个多维数据集,因此您可以添加它
internal void Stop()
{
...
MovingCube.LastCube = MovingCube.CurrentCube;
}
我尝试在GameManager.cs中添加这一行代码,结果出现错误:“索引器'MovingCube.LastCube'的属性无法在此上下文中使用,因为集合访问器无法访问”抱歉,您可以将其放在停止方法的末尾,请检查更新我添加的代码行和相同的当前输出。你在更新方法中删除了该行吗?你也指哪一行?