【问题标题】:How to make the GameObject Stop?如何使游戏对象停止?
【发布时间】:2018-05-28 15:40:26
【问题描述】:

我为敌方 AI 制作了这个简单的脚本。我希望敌人在攻击时停止旋转并朝我们移动。我该怎么写? 代码在下面。

using System.Collections;
using UnityEngine;

public class Chasev2 : MonoBehaviour {

    public Transform Player;
    public float Attack_range=8.5f;
    public float Chase_range=50.0f; //How far ahead the enenmy can see
    public float Distance;
    public float rotation_speed=10.0f;
    private Animator anim;
    public float move_speed=10.0f;


    // Use this for initialization
    void Start () {
        anim = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        anim.SetBool ("Run", true);
        anim.SetBool ("Idle", false);
        anim.SetBool ("Attack", false);
        Distance = (Player.transform.position - transform.position).magnitude;
        if (Distance <= Chase_range && Distance > Attack_range)
        {
            Vector3 Direction = Player.position - transform.position;
            Direction.y = 0;
            transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Direction), rotation_speed * Time.deltaTime);
            transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
            transform.position += transform.forward * move_speed * Time.deltaTime;
        }
        else
        {
            anim.SetBool ("Run", false);
            anim.SetBool ("Idle", true);
            anim.SetBool ("Attack", false);
        }
        if (Distance < Attack_range)
        {
            anim.SetBool ("Run", false);
            anim.SetBool ("Attack", true);
            anim.SetBool ("Idle", false);
        }
    }


void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
            Gizmos.DrawWireSphere (transform.position,Attack_range);

        Gizmos.color = Color.yellow;
            Gizmos.DrawWireSphere (transform.position,Chase_range);

    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    目前无法真正测试这一点,但请尝试将攻击范围条件设置为 else if。

    像这样:

    'if (Distance <= Chase_range && Distance > Attack_range)
    {
        Vector3 Direction = Player.position - transform.position;
        Direction.y = 0;
        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (Direction), rotation_speed * Time.deltaTime);
        transform.eulerAngles = new Vector3 (0, transform.eulerAngles.y, 0);
        transform.position += transform.forward * move_speed * Time.deltaTime;
    }
    else if (Distance < Attack_range)
    {
        anim.SetBool ("Run", false);
        anim.SetBool ("Attack", true);
        anim.SetBool ("Idle", false);
    }
    else
    {
        anim.SetBool ("Run", false);
        anim.SetBool ("Idle", true);
        anim.SetBool ("Attack", false);
    }
    

    `

    我建议您学习使用 FSM 来更轻松、更清洁地发展您的 AI ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      相关资源
      最近更新 更多