【发布时间】:2022-10-07 05:16:39
【问题描述】:
我对编程还很陌生,我的统一游戏的这段代码有问题,基本上发生的事情是第一次我尝试调用 SelectGravity() 它执行 2 或 3 次,之后它似乎正常工作,我不知道为什么。
IsRotating = false 在另一个脚本中被调用,我正在使用 Visual Studio 2019 进行编码,如果这有帮助的话。
void Update()
{
Rotation = Input.GetAxisRaw(\"RotatoWorld\");
if (Rotation != 0 && !IsRotating)
{
IsRotating = true;
SelectGravity();
}
Physics2D.gravity = new Vector2(XGravity, YGravity);
}
对于那些问这是我设置IsRotating false 的脚本
public IEnumerator Rotate90()
{
if(changeGravity.Rotation > 0.1)
{
Direction = 90;
}
else if(changeGravity.Rotation < -0.1)
{
Direction = -90;
}
float timeElapsed = 0;
Quaternion startRotation = transform.rotation;
Quaternion targetRotation = transform.rotation * Quaternion.Euler(0, 0, Direction);
while (timeElapsed < lerpDuration)
{
transform.rotation = Quaternion.Slerp(startRotation, targetRotation, timeElapsed / lerpDuration);
timeElapsed += Time.deltaTime;
yield return null;
}
transform.rotation = targetRotation;
StartCoroutine(CameraShake());
changeGravity.IsRotating = false;
}
我已经这样声明了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeGravity : MonoBehaviour
{
CameraRotation cameraRotation;
PlayerController playerController;
private float XGravity;
private float YGravity;
public float Side;
public float Rotation;
public bool IsRotating;
// Start is called before the first frame update
void Start()
{
cameraRotation = GameObject.FindGameObjectWithTag(\"MainCamera\").GetComponent<CameraRotation>();
playerController = GameObject.FindGameObjectWithTag(\"Player\").GetComponent<PlayerController>();
Side = 0;
YGravity = -9.81f;
XGravity = 0f;
IsRotating = false;
}
除此之外,我不会在任何地方调用此函数或修改此变量。
-
您如何断言
SelectGravity基于一次更新被调用 2 或 3 次? -
对于每次调用
Update,该代码不能多次调用SelectGravity。如果SelectGravity被多次调用,那么您要么是从其他地方调用它,要么是多次调用Update。我们无法帮助您解决这两种情况,因为我们还没有看到相关代码。 -
@Mathias 我是说 SelectGravity 被多次调用,因为我尝试在函数中放置一个 Debug.Log 并在控制台中打印 2 或 3 次(通常为 3,但有时为 2)。 @John 我确定
SelectGravity不会在其他任何地方调用,Update会在每一帧中调用 -
很难像这样说..你能包括整个脚本而不仅仅是sn-ps吗?