简体   繁体   English

Unity中如何让敌人以动量跟随玩家?

[英]How to make an enemy follow the player with momentum in Unity?

I am a beginner in Unity developing a 2D top-down mobile game.我是 Unity 的初学者,正在开发一款 2D 自上而下的手机游戏。 I am trying to create an enemy movement script that mimics the pattern of the leech enemy below:我正在尝试创建一个模仿下面水蛭敌人模式的敌人移动脚本:

在此处输入图像描述

This enemy is constantly trying to move towards the player but even though it can move quite quickly, due to its momentum, you are able to kite it as it cannot make a sharp turn without first taking some time to build speed in another direction.这个敌人不断地试图向玩家移动,但即使它可以移动得很快,由于它的动量,你可以风筝它,因为它不能急转弯,除非先花一些时间在另一个方向上建立速度。

I have created a script for enemies to constantly be targeting the player based on the player's current position but it is too difficult to dodge my enemies as they are able to turn instantly when the player does and maintain a constant speed.我已经为敌人创建了一个脚本,可以根据玩家当前的位置不断地瞄准玩家,但是要躲避我的敌人太难了,因为他们能够在玩家这样做时立即转身并保持恒定的速度。 I would like to balance them to be more like this leech enemy so the player can dodge them by taking advantage of the enemy's current momentum with proper timing.我想让他们平衡得更像这个水蛭敌人,这样玩家就可以在适当的时机利用敌人当前的势头来躲避他们。 How can I create this momentum effect for my enemies?我怎样才能为我的敌人创造这种动量效果?

If you're using Unity's physics here's a way to do this nicely:如果您正在使用 Unity 的物理,这里有一种方法可以很好地做到这一点:

Walkable.cs可步行的.cs

Create a modular component that all walkable game objects will use.创建一个所有可步行游戏对象都将使用的模块化组件。 The purpose of the component is to keep the object moving in the specified direction while stabilising the forces.该组件的目的是保持物体在指定方向上移动,同时稳定力。 It uses the values you configure in the inspector for speed and force.它使用您在检查器中配置的速度和力量值。 It does the movement inside of FixedUpdate as physics movement require it.它在FixedUpdate内部进行运动,因为物理运动需要它。

public class Walkable : MonoBehaviour {

    private const float ForcePower = 10f;

    public new Rigidbody2D rigidbody;

    public float speed = 2f;
    public float force = 2f;

    private Vector2 direction;

    public void MoveTo (Vector2 direction) {
        this.direction = direction;
    }

    public void Stop() {
        MoveTo(Vector2.zero);
    }

    private void FixedUpdate() {
        var desiredVelocity = direction * speed;
        var deltaVelocity = desiredVelocity - rigidbody.velocity;
        Vector3 moveForce = deltaVelocity * (force * ForcePower * Time.fixedDeltaTime);
        rigidbody.AddForce(moveForce);
    }
}

Character.cs字符.cs

This is a simple example of character that will follow a target.这是一个跟随目标的简单角色示例。 Notice how all it's doing is passing the direction to the walkable from inside an Update function.请注意,它所做的只是将方向从Update函数内部传递给 walkable。

public class Character : MonoBehaviour {

    public Transform target;
    public Walkable walkable;

    private void Update() {
        var directionTowardsTarget = (target.position - this.transform.position).normalized;
        walkable.MoveTo(directionTowardsTarget);
    }
}

Configurations配置

By configuring move and force variables you can get a variety of movement styles, some that can move fast but take a long time to ramp up, some that ramp up fast but move slowly overall.通过配置moveforce变量,您可以获得各种运动风格,一些可以快速移动但需要很长时间才能加速,一些快速加速但整体移动缓慢。

You can also play around with with mass and linear drag on the Rigidbody2D to get even more control over the movement style.您还可以在Rigidbody2D上使用质量线性拖动来更好地控制运动风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM