【问题标题】:Unity stack game c#unity stack 游戏 c#
【发布时间】:2019-03-04 11:20:52
【问题描述】:

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

Optimal Output

但是,这就是游戏在我身上所做的事情。

Current Output

Current Output2

方块生成并且可以放置。然而,下一个方块会在与前一个方块相同的 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
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我没有看到任何代码在停止后更改最后一个立方体,因此您可以添加它。

    internal void Stop()
    {
        ...
    
        MovingCube.LastCube = MovingCube.CurrentCube;
    }
    

    【讨论】:

    • 我尝试在 GameManager.cs 中添加该行代码,但出现错误:“索引器的属性 'MovingCube.LastCube' 不能在此上下文中使用,因为设置访问器不可访问”跨度>
    • 对不起,可以放在Stop方法的末尾,请检查更新
    • 我添加了那行代码并且正在发生相同的当前输出。
    • 你删除了Update方法中的那行吗?
    • 你指的是哪一行?
    猜你喜欢
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2012-04-25
    • 2019-02-10
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    相关资源
    最近更新 更多