我不知道这个脚本有什么问题。我尝试将所有变量放入“void Start()”中,但没有任何效果。请帮忙。 据我所知,更新脚本中的所有内容都不是问题。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour{ }
{
Rigidbody PlayerCube;
float Jump = 1;
float Speed = 1;
void Update()
{
// All of this has the controls tilted 45 degrees. Deal with it for now.
if (Input.GetKey(KeyCode.W)) ;
{ PlayerCube.AddForce(0, 0, Speed * 5); }
if (Input.GetKey(KeyCode.S)) ;
{ PlayerCube.AddForce(0, 0, Speed * -5); }
if (Input.GetKey(KeyCode.A)) ;
{ PlayerCube.AddForce(Speed * 5, 0, 0); }
if (Input.GetKey(KeyCode.D)) ;
{ PlayerCube.AddForce(Speed * -5, 0, 0); }
if (Input.GetKeyDown(KeyCode.Space)) ;
{ PlayerCube.AddForce(0, (float)(Jump * 2.5), 0); }
}
}
最佳答案
- 删除
MonoBehaviour
后面的一对括号{ }
- 删除每个
if
语句后的分号;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementScript : MonoBehaviour
{
Rigidbody PlayerCube;
float Jump = 1;
float Speed = 1;
void Update()
{
// All of this has the controls tilted 45 degrees. Deal with it for now.
if (Input.GetKey(KeyCode.W))
{ PlayerCube.AddForce(0, 0, Speed * 5); }
if (Input.GetKey(KeyCode.S))
{ PlayerCube.AddForce(0, 0, Speed * -5); }
if (Input.GetKey(KeyCode.A))
{ PlayerCube.AddForce(Speed * 5, 0, 0); }
if (Input.GetKey(KeyCode.D))
{ PlayerCube.AddForce(Speed * -5, 0, 0); }
if (Input.GetKeyDown(KeyCode.Space))
{ PlayerCube.AddForce(0, (float)(Jump * 2.5), 0); }
}
}
为了简单起见,修改您的 Update()
方法
void Update()
{
PlayerCube.AddForce(-Speed * 5 * Input.GetAxisRaw("Vertical"), 0, Speed * 5 * Input.GetAxisRaw("Horizontal"));
if (Input.GetKeyDown(KeyCode.Space))
{
PlayerCube.AddForce(0, (float)(Jump * 2.5), 0);
}
}
关于c# - 变量设置行会干扰顶级语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73242442/