C# 对象移动不工作
到目前为止,我已经尝试用Unity创建了两个游戏,一个是2D,另一个是3D。在我的2D游戏中,我对移动物体没有任何问题 这是我将播放器对象向左移动的代码,在左侧UI按钮上单击:C# 对象移动不工作,c#,unity3d,C#,Unity3d,到目前为止,我已经尝试用Unity创建了两个游戏,一个是2D,另一个是3D。在我的2D游戏中,我对移动物体没有任何问题 这是我将播放器对象向左移动的代码,在左侧UI按钮上单击: float moveSpeed = 2f; //....... //....... public void SheWalksL() { AniWalkL (); // Animation GetComponent<Rigidbody2D> ().velocity = new Vector2 (
float moveSpeed = 2f;
//.......
//.......
public void SheWalksL()
{
AniWalkL (); // Animation
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D> ().velocity.y); // Move Left
}
唯一不同的是,在3D游戏中,我单独创建了运动矢量,并将其指定给对象速度,并直接提供矢量的X值,而不使用变量。我还有一个“Fire”按钮,当按下它的时候,它的功能非常好,所以按钮事件调用不会有问题。有人能指出我在3D代码中犯了什么错误,使其无法工作。运行此程序时,速度的值是多少?哦,对不起,速度是10。公共浮动速度=10;用addForce代替那个。它会更现实,它会解决你的问题,使用addForce,但仍然不起作用(?)。GetComponent().AddForce(movemnt*速度);在ButtonLevel中添加Debug.log(GetComponent().velocity),并与我们共享结果。如果您有任何合理的理由,请添加对象的inspector屏幕截图。
public void ButtonMoveL()
{
Vector3 movemnt = new Vector3(-2.0f, GetComponent<Rigidbody>().velocity.y, 0);
GetComponent<Rigidbody>().velocity = movemnt * speed; // Move Left(Vantage point: looking down at the scene, along the Y-axis. So, Z is aligned vertically and X is aligned horizontally)
}