0

这是我的玩家移动的代码:

var animator : Animator;

function Start () {
    animator = GetComponent("Animator");
}


var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var jumpheight : int = 2;
var speed : int = 2;
var isGrounded : int = 0;

// Checks if player is in air or ground

var Jumptest : int = 0;

// Checks if player has jumped

function Update () {

    animator.SetInteger("Direction", 4);

    if (Input.GetKey(moveUp) && isGrounded == 0)
    {
        rigidbody2D.velocity.y = jumpheight;
        isGrounded = 1;
        animator.SetInteger("Direction", 2);
    }


    else if (Input.GetKey(moveLeft))
    {
        transform.position += Vector3.left * speed * Time.deltaTime;
        animator.SetInteger("Direction", 3);
    }
    else if (Input.GetKey(moveRight))
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
        animator.SetInteger("Direction", 1);
    }

}

这是弹丸运动的代码:

#pragma strict

var speed  = 8.0;
var time = 0.0;


function Start () {

}

var moveRight : KeyCode;
var moveLeft : KeyCode;

function Update () {
    time += Time.deltaTime;
    if(time > 3){
        Destroy(gameObject);
    }

    transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
}

此刻,弹丸向右射击。我希望能够朝玩家所面对的方向射击它们。

我尝试了 if key down a 和 if key down d 但是当玩家不移动并相对于玩家移动时弹丸停止(如果我向左射击,然后将我的玩家向右移动,弹丸反转方向)。

有人知道吗?

4

1 回答 1

0

当检查玩家移动时,左右键是否按下,你创建一个简单的整数来跟踪方向

...
var isRight: int=1 ;//assuming player starts with facing in the right direction
...

然后,在检查按键时,适当地设置这个变量的值

...
else if (Input.GetKey(moveLeft)&&!Input.GetKey(moveRight))
{
    isRight = -1;
    ...
}
else if (Input.GetKey(moveRight)&&!Input.GetKey(moveLeft))
{
    isRight = 1;  
    ...
}

最后,在为弹丸设置平移时,只需将速度乘以这个新变量

    transform.Translate(Vector3.right * speed * isRight * Time.deltaTime, Space.Self);
于 2014-05-06T11:45:33.847 回答