【问题标题】:Unity 2D, C# - Creating an AI that both follows the player and flutters around himUnity 2D, C# - 创建一个既能跟随玩家又能在他周围飘动的 AI
【发布时间】:2018-05-21 02:56:09
【问题描述】:

首先,总的来说,我对编码非常陌生,但我只是把它当作一种爱好,所以请原谅我的普遍缺乏理解。

我正在制作一个横向卷轴平台游戏,其中一个小角色跟随你并为你提供建议、照亮道路、填充传说等。想想塞尔达传说中的 Navi。我已经有一个可以跟踪玩家的功能脚本,我将在下面发布。

但是,我不能让它懒洋洋地在角色周围漂浮,而不仅仅是坐着不动。

public class WillOWispFollowPlayer : MonoBehaviour
{
public Transform target;

public float smoothSpeed = 0.05f;
public Vector3 offset;


private void FixedUpdate()
  {

    Vector3 desiredPosition = target.position + offset;
    Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
    transform.position = smoothedPosition;

  }
}

【问题讨论】:

    标签: c# unity3d animation sprite


    【解决方案1】:

    我还没有涉足游戏开发,但我的方法是实现一些随机位置,偏向玩家或靠近玩家的目标位置。

    因此,假设以半均匀的间隔,您生成一个新的随机位置或方向供您的同伴移动,该距离应该相对接近同伴的当前位置。为了防止你的同伴随机飞走,落入日落,你还应该确保这个随机位置或方向偏向玩家位置,例如,它离玩家越远,更偏向于向玩家移动。

    【讨论】:

    • 这可以工作,但它似乎有点复杂。我只需要 AI 在角色空闲时不要完全静止不动。我的一个想法是给 AI 应用一个刚体,然后在随机方向轻轻地施加力。我会弄乱一些东西,看看我想出了什么。
    【解决方案2】:

    经过一段时间的努力,并查看了其他人的代码,这就是我想出的。它在原始代码中添加了两个函数。一种是在 FixedUpdate 中应用的具有可调节加速和减速的随机方向函数。另一个是在一个单独的随机方向上的短暂速度爆发。效果比我最初想要的更飘飘,也没有飘浮的漂移效果,但它确实有效。如果有人有任何想法可以让这变得不那么紧张,更流畅,飘逸的效果,我愿意接受建议。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    [RequireComponent(typeof(Rigidbody2D))]
    public class WillOWispFollowPlayer : MonoBehaviour
    {
    
    public Transform target;
    
    public float smoothSpeed = 0.05f;
    public float smoothSpeedSlow = 0.02f;
    public Vector3 offset;
    public float hoverSpeed = 10000f;
    public Rigidbody2D rb;
    public static System.Timers.Timer hoverTimer;
    
    public float acceleration;
    public float deceleration;
    
    private Rigidbody2D physics;
    private Vector2 direction;
    
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        InvokeRepeating("HoverDirection", 0, 1); //calling the DoverDirection every second
    
        physics = GetComponent<Rigidbody2D>();
        physics.gravityScale = 0;
        physics.drag = deceleration;
    }
    
    private void FixedUpdate()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        Vector3 smoothedPositionSlow = Vector3.Lerp(transform.position, desiredPosition, smoothSpeedSlow);
    
        //setting up a "wander zone" where the Will'o'the'Wisp is less restricted within a defined distance of the player
        if (Vector3.Distance((target.position + offset), this.transform.position) >= .5)
        {
            transform.position = smoothedPosition;
        }
        else
        {
            transform.position = smoothedPositionSlow;
        }
    
        //Random direction generator This results in a "flutter" effect
        direction = UnityEngine.Random.insideUnitCircle;
    
        Vector2 directionalForce = direction * acceleration;
        physics.AddForce(directionalForce * Time.deltaTime, ForceMode2D.Impulse);
    }
    
    //A regular and prominant directional change, resulting in short darting motions around the target position
    void HoverDirection() 
    {
        rb.velocity = Random.onUnitSphere * hoverSpeed;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多