Unity foreach 错误

问题描述 投票:0回答:1

所以我正在开发一款骰子游戏,起初我认为这会很容易,但我刚刚发现了一个主要的“错误”。 所以这是我遇到问题的主要脚本:

using System.Collections.Generic;
using UnityEngine;

public class BoardController : MonoBehaviour
{
    #region VAR
    [SerializeField] private List<Transform> diceRestPositions = new List<Transform>();
    [SerializeField] private List<Transform> dices = new List<Transform>();
    [SerializeField] private Transform throwPosition;
    [SerializeField] private float throwRange;
    #endregion

    #region MONO
    private void Update()
    {
        LaunchDices();
        if(Input.GetKeyDown(KeyCode.Alpha2))
        {
            ResetDicesPos();
        }
    }
    private void Start()
    {
        
    }
    #endregion

    #region CUSTOM

 
    private Vector3 GetRandomThrowPos()
    {
        return new Vector3(Random.Range(throwPosition.position.x- throwRange, throwPosition.position.x + throwRange), 1.2f, Random.Range(throwPosition.position.z - throwRange, throwPosition.position.z + throwRange));
    }

    private void ResetDicesPos()
    {
        StopDiceMovement();
        for (int i = 0; i < diceRestPositions.Count; i++)
        {
            dices[i].position = diceRestPositions[i].localPosition;
            dices[i].rotation = diceRestPositions[i].localRotation;
        }
    }
    private void StopDiceMovement()
    {
        foreach(Transform dice in dices)
        {
            if(dice.TryGetComponent<Rigidbody>(out Rigidbody rb))
            {
                rb.velocity = Vector3.zero;
                dice.rotation = Quaternion.identity;
                rb.angularVelocity = Vector3.zero;
            }
        }
    }
    private void LaunchDices()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1)) 
        {
            StopDiceMovement();
            foreach (Transform dice in dices)
            {
                dice.position = GetRandomThrowPos();
                if(dice.TryGetComponent<DiceAddForce>(out DiceAddForce comp))
                {
                    comp.ApplyRandomForce();
                }
            }
        }
        
    }
    #endregion    
}

因此,解释一下“diceRestPosition”是一个骰子在发射前静止的列表。 “dices”只是一个带有骰子变换的列表。 “ throwPosition”只是 throwingArea 的中点,“ throwRange”定义了范围。

因此,当我按下“Alpha1”时,5 个骰子会从随机位置发射,并带有rigidbody.AddForce 和rigidbody.AddTorque,因此它们正在旋转。 当我按下“alpha2”时,每个骰子都会返回到“diceRestPosition”中定义的位置。 但有时当我使用这些按钮时,会出现一些错误,例如: -> 当我双击 1 有时他们只是 doubleJump 就像这样 StopDiceMovement() 不起作用 -> 有时骰子是从“diceRestPosition”发射的,而不是随机的 pos -> 最后一个是当我按 2 且骰子仍在移动时,它们只是停止但不会返回到“diceRestPosition”

我在编程方面很新,不太确定该怎么做,但首先我认为这是资源错误,例如 foreach 太重并且被跳过?但我只有 5 个骰子,我不认为这是造成这种情况的原因。 我还看到,当骰子移动时,当它们仍在移动/滚动或停止倾斜时更有可能准确。 如果您阅读本文,我非常感谢您的关注,并请求您帮助我解决这个奇怪的错误:)

c# unity-game-engine foreach game-physics rigid-bodies
1个回答
0
投票

我不知道Unity如何处理按键(板)“重复计数”,但是如果您没有“锁定”或以其他方式跳过/处理“繁忙”代码,那么您可能会遇到重新进入问题键的速度比您处理它们的速度快。确认“预期”和意外行为的常用方法是添加适当的调试语句。断点在事件处理程序中并不那么“友好”。

因此,除了代码周围的“锁”(C# 关键字)之外,您还可以设置、取消设置(最后尝试)和测试繁忙标志以避免重新进入。

© www.soinside.com 2019 - 2024. All rights reserved.