【发布时间】:2014-10-26 22:48:49
【问题描述】:
我是 Unity2D 的新手,我正试图让我的精灵根据它所面对的方向移动。我可以通过按 A 或 D 键来获得旋转,但是当根据我的精灵面向的方向向前移动时,我遇到了错误
我当前的代码:
#pragma strict
var moveForward : KeyCode;
var turnRight : KeyCode;
var turnLeft : KeyCode;
var fire : KeyCode;
var speed : float = 4;
var turnSpeed : float = 2;
function Update () {
// When the user wants to jet pulse forward.
if(Input.GetKey(moveForward)){
// NEEDS FIXING HERE!!! <<<<<
rigidbody2D.velocity.x = Mathf.Cos(rigidbody2D.rotation) * speed;
rigidbody2D.velocity.y = Mathf.Sin(rigidbody2D.rotation) * speed;
}
// When we rotate the sprite.
if(Input.GetKey(turnRight)){
transform.Rotate(0.0f, 0.0f, -5.0f);
}else if(Input.GetKey(turnLeft)){
transform.Rotate(0.0f, 0.0f, 5.0f);
}
}
【问题讨论】:
标签: unity3d rotation unityscript