我正在制作一個 2d 無盡的跑步者,其中我已經為玩家的重力和運動實作了一些代碼。當我通過下面附加的腳本向玩家添加重力和速度并啟用播放模式時,一個名為“DontDestroyOnLoad”的場景出現在層次結構視窗中,并且它有一個名為 [Debug Updater] 的物件作為附加到它的子項。播放器的腳本在這里:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HeroCharacterController : MonoBehaviour
{
[SerializeField] LayerMask groundLayers;
private float gravity = -50f;
private CharacterController characterController;
private Vector3 velocity;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
characterController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
///Is Grounded
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);
if(!isGrounded && velocity.y <0)
{
velocity.y = 0;
}
else
{
///Add Gravity
velocity.y = gravity * Time.deltaTime;
}
}
}
附加到除錯更新程式的名為“DebugUpdater”的腳本在這里:
namespace UnityEngine.Rendering
{
class DebugUpdater : MonoBehaviour
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void RuntimeInit()
{
if (!Debug.isDebugBuild || FindObjectOfType<DebugUpdater>() != null)
return;
var go = new GameObject { name = "[Debug Updater]" };
go.AddComponent<DebugUpdater>();
DontDestroyOnLoad(go);
}
void Update()
{
DebugManager.instance.UpdateActions();
if (DebugManager.instance.GetAction(DebugAction.EnableDebugMenu) != 0.0f)
DebugManager.instance.displayRuntimeUI = !DebugManager.instance.displayRuntimeUI;
if (DebugManager.instance.displayRuntimeUI && DebugManager.instance.GetAction(DebugAction.ResetAll) != 0.0f)
DebugManager.instance.Reset();
}
}
}
這是導致的錯誤,我的腳本無法正常作業,因為播放器沒有下降或移動。錯誤:
ArgumentException: SceneManager.SetActiveScene failed; the internal DontDestroyOnLoad scene cannot be set active.
UnityEngine.SceneManagement.SceneManager.SetActiveScene (UnityEngine.SceneManagement.Scene scene) (at <028e4d71153d4ed5ac6bee0dfc08aa3b>:0)
UnityEditor.SceneHierarchy.TreeViewItemDoubleClicked (System.Int32 instanceID) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.HandleUnusedMouseEventsForItem (UnityEngine.Rect rect, UnityEditor.IMGUI.Controls.TreeViewItem item, System.Int32 row) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.DoItemGUI (UnityEditor.IMGUI.Controls.TreeViewItem item, System.Int32 row, System.Single rowWidth, System.Boolean hasFocus) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.IterateVisibleItems (System.Int32 firstRow, System.Int32 numVisibleRows, System.Single rowWidth, System.Boolean hasFocus) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.IMGUI.Controls.TreeViewController.OnGUI (UnityEngine.Rect rect, System.Int32 keyboardControlID) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchy.DoTreeView (System.Single searchPathHeight) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchy.OnGUI (UnityEngine.Rect rect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchyWindow.DoSceneHierarchy () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.SceneHierarchyWindow.OnGUI () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.HostView.Invoke (System.String methodName) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition, UnityEngine.Rect viewRect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect viewRect, UnityEngine.Rect dockAreaRect) (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEditor.DockArea.OldOnGUI () (at <bd70c40e01f641bdb7d836e1e97755bc>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUI (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.IMGUIContainer.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatchUtilities.PropagateEvent (UnityEngine.UIElements.EventBase evt) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.MouseEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEventQueue () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.OpenGate () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcherGate.Dispose () (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.UIElements.UIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <a6a8a08b59d34373858eada2d852ad38>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <023156577e4f4156adf0f4b3a3fedf85>:0)
此外,我還附上了我在游戲模式和場景模式下的游戲截圖。截圖:
提前謝謝!

uj5u.com熱心網友回復:
這會導致問題:
isGrounded = Physics.CheckSphere(transform.position, 0.1f, groundLayers, QueryTriggerInteraction.Ignore);
解決方案是對這個方法只使用兩個引數:
Physics.CheckSphere(transform.position, 0.1f);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329217.html
下一篇:統一夾緊四元數旋轉
