简单的Unity角色控制器问题

问题描述 投票:0回答:1

我正在制作 2D 平台游戏,为了使角色移动,我使用这个 无效更新()

{
move = Input.GetAxis("Horizontal");

rb.velocity = new Vector2(move * speed, rb.velocity.y);
}

但是当我尝试通过添加 X 力来制作冲刺机制时,它只是忽略了它,因为 X 速度始终是固定的...... 我该怎么办?

我试过这个

Vector2 input = new Vector2(move*speed, 0)
rb.velocity += input;

rb.velocity * input;

还有许多其他人..没有任何帮助..

c# unity-game-engine
1个回答
0
投票

每一帧都会调用更新,因此为一帧添加大量力量不会有太大作用,这里有更好的方法来做到这一点。 首先创建一些变量:

private bool dashing = false;
private float dashTimer = 0;
private int dashDirection;
private float dashCooldownTimer = 0;
public float dashDuration = 0.5f;
public float dashCooldown = 1f;
public float dashForce = 8f;

public float moveSpeed = 5f;
// assign x input to a variable so it can be accessed anywhere in the script.
private float xInput;

现在你需要一种方法来设置玩家当前方向,我假设你使用一个脚本来翻转玩家的 x 比例,你可以添加创建这个新函数并将其添加到更新中:

// add this to the top of the update function
void Update() {
    // check if the scale is bigger or smaller than 0 and assign the direction.
    if (transform.localScale > 0) {
        dashDirection = 1;
    }
    else if (transform.localScale < 0) {
        dashDirection = -1;
    }
}

注意:如果您以不同的方式翻转播放器,请告诉我,因为这不起作用。 现在我们需要检查玩家是否冲刺,因此您可以将其添加到我们之前的代码下面:

// add this to the top of the update function
void Update() {
    // check if the scale is bigger or smaller than 0 and assign the direction.
    if (transform.localScale > 0) {
        dashDirection = 1;
    }
    else if (transform.localScale < 0) {
        dashDirection = -1;
    }

    // for this example I check whether the shift key is pressed, but you can change this depending on what you want to make the player dash, also check if the cooldown has finished
    if (Input.GetKeyDown(KeyCode.LeftShift) && dashCooldownTimer <= 0) {
        dashCooldownTimer = dashCooldown;
        dashing = true;
        dashTimer = 0;
    }
}

现在破折号检查器已完成,我们只需要实现破折号即可,为此,您可以将此代码添加到

FixedUpdate()

void FixedUpdate() {
    if (dashing && dashTimer >= 0) {
        rb.velocity = new Vector2(dashForce * dashDirection, 0);
    }
    if (dashing && dashTimer < dashDuration) {
        dashTimer += Time.deltaTime;
    }
    else {
        dashing = false;
        dashTimer = 0;
    }
    if (dashCooldownTimer > 0) {
        dCooldown -= Time.deltaTime;
    }
}

另外,确保只在不冲刺时应用移动,否则你会得到抖动效果。

// add this to the top of the update function
void Update() {
    // check if the scale is bigger or smaller than 0 and assign the direction.
    if (transform.localScale > 0) {
        dashDirection = 1;
    }
    else if (transform.localScale < 0) {
        dashDirection = -1;
    }

    // for this example I check whether the shift key is pressed, but you can change this depending on what you want to make the player dash, also check if the cooldown has finished
    if (Input.GetKeyDown(KeyCode.LeftShift) && dashCooldownTimer <= 0) {
        dashCooldownTimer = dashCooldown;
        dashing = true;
        dashTimer = 0;
    }

    xInput = Input.GetAxis("Horizontal");
    if (!dashing) rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);
}

这应该可以正常工作,但是,是的,破折号比看起来要复杂得多。如果您需要任何帮助,请告诉我:)

© www.soinside.com 2019 - 2024. All rights reserved.