如何优化我的角色移动的Unity C#代码



我一直在想如何让这个特定的运动系统工作,我让它工作了(耶!(,但坏消息是它没有被优化。我有很多复制粘贴,老实说,我有点不好意思发布这个,但它在这里。如果你看到任何明显的错误,请告诉我我做错了什么(你会的(。我对编程还很陌生,所以我想在养成坏习惯之前先改掉它们。

作为参考,下面发布的代码是为我的玩家角色。它引用了当前附加到相机的另一个脚本中的一个名为camDirection的浮点值,它以1为增量从-3变为+3。

这里的基本想法是,当玩家按下两个按钮中的一个将相机围绕玩家移动90度时,我希望玩家的移动输入在键盘上切换方向,一个用于顺时针旋转,另一个用于逆时针旋转。当相机朝北时,我们有标准的WASD控件(上为W,左为A,下为S,右为D(,我将其分配给一个名为PlayerN的动作图。

现在,如果相机朝东,动作图将更改为PlayerW,其中向上是D,向左是W,向右是S,向下是A。如果这听起来很奇怪,请逆时针旋转键盘90度,看看你的WASD键,你可能会看到我来自哪里。我不知道这有多优化,但这在当时对我来说是有意义的,所以我坚持了下来,我无法让unity的ApplyBindingOverride或类似的东西发挥作用,但我确信我使用错误了。除了PlayerSPlayerE,我总共有4个动作图,每个方向一个。

不管怎样,它就在这里。我要在帖子下面分解一些混乱:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public InputMaster controls;
private float playerSpeed = 4f;
Vector2 moveN;
Vector2 moveE;
Vector2 moveS;
Vector2 moveW;
Vector2 moveW2;
Vector2 moveS2;
Vector2 moveE2;
public CameraRotate refScript;
void Awake()
{
controls = new InputMaster();
controls.PlayerN.Movement.performed += ctx => moveN = ctx.ReadValue<Vector2>();
controls.PlayerN.Movement.canceled += ctx => moveN = Vector2.zero;
controls.PlayerE.Movement.performed += ctx => moveE = ctx.ReadValue<Vector2>();
controls.PlayerE.Movement.canceled += ctx => moveE = Vector2.zero;
controls.PlayerE.Movement.performed += ctx => moveE2 = ctx.ReadValue<Vector2>();
controls.PlayerE.Movement.canceled += ctx => moveE2 = Vector2.zero;
controls.PlayerS.Movement.performed += ctx => moveS = ctx.ReadValue<Vector2>();
controls.PlayerS.Movement.canceled += ctx => moveS = Vector2.zero;
controls.PlayerS.Movement.performed += ctx => moveS2 = ctx.ReadValue<Vector2>();
controls.PlayerS.Movement.canceled += ctx => moveS2 = Vector2.zero;
controls.PlayerW.Movement.performed += ctx => moveW = ctx.ReadValue<Vector2>();
controls.PlayerW.Movement.canceled += ctx => moveW = Vector2.zero;
controls.PlayerW.Movement.performed += ctx => moveW2 = ctx.ReadValue<Vector2>();
controls.PlayerW.Movement.canceled += ctx => moveW2 = Vector2.zero;
}
void Update()
{
Vector2 inputVectorN = controls.PlayerN.Movement.ReadValue<Vector2>();
Vector2 inputVectorE = controls.PlayerE.Movement.ReadValue<Vector2>();
Vector2 inputVectorS = controls.PlayerS.Movement.ReadValue<Vector2>();
Vector2 inputVectorW = controls.PlayerW.Movement.ReadValue<Vector2>();
Vector2 inputVectorE2 = controls.PlayerE.Movement.ReadValue<Vector2>();
Vector2 inputVectorS2 = controls.PlayerS.Movement.ReadValue<Vector2>();
Vector2 inputVectorW2 = controls.PlayerW.Movement.ReadValue<Vector2>();
switch (refScript.camDirection)
{
case -3:
Debug.Log("Facing East");
inputVectorE = new Vector2(moveE.x, moveE.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorE = new Vector3();
finalVectorE.x = inputVectorE.x;
finalVectorE.z = inputVectorE.y;
transform.Translate(finalVectorE, Space.World);
break;
case -2:
Debug.Log("Facing South");
inputVectorS = new Vector2(moveS.x, moveS.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorS = new Vector3();
finalVectorS.x = inputVectorS.x;
finalVectorS.z = inputVectorS.y;
transform.Translate(finalVectorS, Space.World);
break;
case -1:
Debug.Log("Facing West");
inputVectorW = new Vector2(moveW.x, moveW.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorW = new Vector3();
finalVectorW.x = inputVectorW.x;
finalVectorW.z = inputVectorW.y;
transform.Translate(finalVectorW, Space.World);
break;
case 0:
Debug.Log("Facing North");
inputVectorN = new Vector2(moveN.x, moveN.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorN = new Vector3();
finalVectorN.x = inputVectorN.x;
finalVectorN.z = inputVectorN.y;
transform.Translate(finalVectorN, Space.World);
break;
case 1:
Debug.Log("Facing East");
inputVectorE2 = new Vector2(moveE2.x, moveE2.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorE2 = new Vector3();
finalVectorE2.x = inputVectorE2.x;
finalVectorE2.z = inputVectorE2.y;
transform.Translate(finalVectorE2, Space.World);
break;
case 2:
Debug.Log("Facing South");
inputVectorS2 = new Vector2(moveS2.x, moveS2.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorS2 = new Vector3();
finalVectorS2.x = inputVectorS2.x;
finalVectorS2.z = inputVectorS2.y;
transform.Translate(finalVectorS2, Space.World);
break;
case 3:
Debug.Log("Facing West");
inputVectorW2 = new Vector2(moveW2.x, moveW2.y) * Time.deltaTime * playerSpeed;
Vector3 finalVectorW2 = new Vector3();
finalVectorW2.x = inputVectorW2.x;
finalVectorW2.z = inputVectorW2.y;
transform.Translate(finalVectorW2, Space.World);
break;
}

}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}

我认为Switch可能是这里最糟糕的罪犯,我不知道如何压缩我的移动代码,所以我只是为每种情况复制了很多次。然而,一旦我这样做了4次(对于情况-3、-2-1和0(,它就开始给我错误,这是因为它与我在以前的情况下所做的代码相冲突。

例如,当case为-3或1时,玩家将面向东方,这意味着我应该运行相同的代码。然而,它开始给我错误,这时我注意到Switch语句中不能有相同的变量名(这是我第一次使用(。因此,我很快创建了名为moveE2inputVectorE2finalVectorE2的变量,除了north之外,我对每个方向都这样做,因为它只在switch语句中显示一次。

我写这篇文章的一个重要原因是,我不一定希望这是我角色的最后一个动作系统,我不知道如何轻松地为这个代码创建更模块化的东西,或者在未来做类似的事情时。目前,如果我想改变这个动作,我必须深入案例并改变7次。效率不是很高。

无论如何,感谢您的阅读,我真的很感谢您为精简此代码并使其在未来更容易使用而提供的任何建议或技巧。

好吧,我对它进行了一些优化。这主要是为了帮助任何通过谷歌或其他方式找到这一点的人。我学会了如何使用列表并添加了它们。这使我的脚本更容易使用,也使我能够摆脱额外的方向(E2S2W2(,这很好。

好消息是它现在更加模块化了,这很好。我有点担心我在void update()有太多事情要做,但我想我们拭目以待。

我还把switch语句压缩了一点,因为我注意到我现在在它们中发生了相同的事情。

即使有了这些改进,我确信它可以得到更多的优化,但目前这正是我需要的:D希望这能帮助到一些人。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{  
private Transform _transform;
//this is the new transform object I added to help with optimization. 
public InputMaster controls;
public CameraRotate refScript;
private float playerSpeed = 4f;
Vector2 _move;
Vector2 _inputVector;
Vector3 _finalVector;
public List<Vector2> moveDir = new List<Vector2>();
public List<Vector2> inputVectorDir = new List<Vector2>();
public List<Vector3> finalVectorDir = new List<Vector3>();
Vector2 moveN;
Vector2 moveE;
Vector2 moveS;
Vector2 moveW;
void Awake()
{
moveDir.Add(moveN);
moveDir.Add(moveE);
moveDir.Add(moveS);
moveDir.Add(moveW);
_transform = transform;
controls = new InputMaster();
//north
controls.PlayerN.Movement.performed += ctx => moveDir[0] = ctx.ReadValue<Vector2>();
controls.PlayerN.Movement.canceled += ctx => moveDir[0] = Vector2.zero;
//east
controls.PlayerE.Movement.performed += ctx => moveDir[1] = ctx.ReadValue<Vector2>();
controls.PlayerE.Movement.canceled += ctx => moveDir[1] = Vector2.zero;
//south
controls.PlayerS.Movement.performed += ctx => moveDir[2] = ctx.ReadValue<Vector2>();
controls.PlayerS.Movement.canceled += ctx => moveDir[2] = Vector2.zero;
//west
controls.PlayerW.Movement.performed += ctx => moveDir[3] = ctx.ReadValue<Vector2>();
controls.PlayerW.Movement.canceled += ctx => moveDir[3] = Vector2.zero;
}
void MovementScript()
{
_inputVector = new Vector2(_move.x, _move.y) * Time.deltaTime * playerSpeed;
_finalVector.x = _inputVector.x;
_finalVector.z = _inputVector.y;
_transform.Translate(_finalVector, Space.World);
}
void Update()
{
Vector2 inputVectorN = controls.PlayerN.Movement.ReadValue<Vector2>();
Vector2 inputVectorE = controls.PlayerE.Movement.ReadValue<Vector2>();
Vector2 inputVectorS = controls.PlayerS.Movement.ReadValue<Vector2>();
Vector2 inputVectorW = controls.PlayerW.Movement.ReadValue<Vector2>();
inputVectorDir.Add(inputVectorN);
inputVectorDir.Add(inputVectorE);
inputVectorDir.Add(inputVectorS);
inputVectorDir.Add(inputVectorW);
Vector3 finalVectorN = new Vector3();
Vector3 finalVectorE = new Vector3();
Vector3 finalVectorS = new Vector3();
Vector3 finalVectorW = new Vector3();
finalVectorDir.Add(finalVectorN);
finalVectorDir.Add(finalVectorE);
finalVectorDir.Add(finalVectorS);
finalVectorDir.Add(finalVectorW);
switch (refScript.camDirection)
{
case 0:
Debug.Log("Facing North");
_move = moveDir[0];
_inputVector = inputVectorDir[0];
_finalVector = finalVectorDir[0];
MovementScript();
break;
case -3:
case 1:
Debug.Log("Facing East");
_move = moveDir[1];
_inputVector = inputVectorDir[1];
_finalVector = finalVectorDir[1];
MovementScript();
break;
case -2:
case 2:
Debug.Log("Facing South");
_move = moveDir[2];
_inputVector = inputVectorDir[2];
_finalVector = finalVectorDir[2];
MovementScript();
break;
case -1:
case 3:
Debug.Log("Facing West");
_move = moveDir[3];
_inputVector = inputVectorDir[3];
_finalVector = finalVectorDir[3];
MovementScript();
break;
}
}
private void OnEnable()
{
controls.Enable();
}
private void OnDisable()
{
controls.Disable();
}
}

最新更新