【问题标题】:Dashing in Unity 2D using the Rigidbody Component使用刚体组件在 Unity 2D 中冲刺
【发布时间】:2021-01-26 11:41:36
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{

    public float movementSpeed;
    public Rigidbody2D Rigidbody;
    private Vector2 moveDirection;
    public Transform Player;
    void Start()
    {
        
    }

    void Update()
    {
        ProcessInputs();
    } void FixedUpdate()
    {
        move();
    }
    void ProcessInputs()
    {
        float moveX = Input.GetAxisRaw("Horizontal");
        float moveY = Input.GetAxisRaw("Vertical");

        moveDirection = new Vector2(moveX, moveY).normalized;
    }
     void move()
    {
        Rigidbody.velocity = new Vector2(moveDirection.x * movementSpeed, moveDirection.y * movementSpeed);
    }

}


所以我需要一些提示,所以我想在这里问一下,我想在我的 Top-Down-Shooter 中实现冲刺,但我在 Youtube 上没有找到任何可以正确使用我的代码的代码我也尝试过移动玩家位置+2 到想要的方向,但我无法解决。 如果你们能帮助我,我会很高兴。

【问题讨论】:

    标签: c# unity3d game-development rigid-bodies


    【解决方案1】:

    很高兴您将 FixedUpdate() 用于物理 - 许多初学者使用错误。 但是不要手动设置速度。使用加力。 它将有效地设置速度,但会自动考虑物体的质量并允许加速而不是立即加速。 这是更合乎逻辑的方法,因为您通过向身体施加力来移动。

    对于 Dashing,您可以为少量的物理帧(也就是少量的 FixedUpdate 调用)添加显着更高的力。当字符开始冲刺时,您可以使用设置为数字的倒计时。 在 FixedUpdate 中,您递减计数器,只要它 >0,您就应用冲刺力。

    【讨论】:

    • 哦,好的,谢谢,这是有道理的,但是我如何对其进行编码,以便它只持续一秒钟而不是只要我按下它,我应该使用 input.getkeydown(Keycode.Space) 还是我应该做点别的吗?
    • 是的,update() 中的函数将计数器设置为目标值。你也可以使用docs.unity3d.com/Manual/Coroutines.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多