創作內容

1 GP

AI(觀察附近+看到敵人向前攻擊+太遠時停下來)+NavMeshAgent

作者:麋瑀│2017-04-26 21:19:28│巴幣:2│人氣:803
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Chase : MonoBehaviour {

public Transform player;//指定目標
static Animator anim;//動畫面板
public Transform head;//本體的視線(頭看過去的位子)
bool pursuing = false;
private NavMeshAgent nav;

// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
nav = GetComponent<NavMeshAgent> ();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;//本體看目標的幅度

float angle = Vector3.Angle(direction, head.up);//【up->綠軸(Y),right->紅軸(X),forward->藍軸(Z)】
if (Vector3.Distance (player.position, this.transform.position) < 10 && (angle < 30 || pursuing)) //目標接近本體的範圍 , Vector3.Angle角度小於30時(angle < 30)
{
pursuing = true;
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (direction), 0.1f); //跟著目標的方向

anim.SetBool("isIdle", false);
if (direction.magnitude > 5) //跟目標之間的距離
{
nav.SetDestination (player.transform.position);// nav 追隨指定目標
this.transform.Translate(0, 0, 0.05f);//跟著目標移動位子與速度(Y,X,Z)
anim.SetBool("isWalk", true);
anim.SetBool("isAttack", false);
}
else
{
anim.SetBool("isAttack", true);
anim.SetBool("isWalk", false);
}

}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalk", false);
anim.SetBool("isAttack", false);
pursuing = false;
nav.SetDestination (transform.position); //nav 歸零
}
}
}

引用網址:https://home.gamer.com.tw/TrackBack.php?sn=3556897
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

1喜歡★k940714675 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:AI-巡邏(指定地點)-... 後一篇:腳本 開與關...

追蹤私訊切換新版閱覽

作品資料夾

blacktor讀者
【勇氣爆發】Chapter-999.情慾爆發的夏天-B......啊,ATF給勇跟史密斯發結婚禮金了呢......看更多我要大聲說昨天22:29


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】