【AI】角色的自主移动(2)——靠近、离开、抵达、追逐、逃避
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 靠近 /// </summary> public class SteeringForSeek :Steering { //目标物体 public GameObject target; //预期速度 private Vector3 desiredVelocity; //获取AI角色 private Vehicle m_vehicle; //最大 速度 private float maxSpeed; //是否在2d平面 private bool isPlanar; void Start() { m_vehicle = GetComponent<Vehicle>(); maxSpeed = m_vehicle.maxSpeed; isPlanar = m_vehicle.isPlanar; } //计算力 public override Vector3 Force() { desiredVelocity=(target.transform.position-transform.position).normalized*maxSpeed; if (isPlanar) { desiredVelocity.y=0; } return (desiredVelocity - m_vehicle.velocity); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 离开 /// </summary> public class SteeringForFlee : Steering{ public GameObject target; //设置危险距离 public float fearDistance = 20; private Vector3 desiredVelocity; private Vehicle m_vehicle; private float maxSpeed; void Start() { m_vehicle = GetComponent<Vehicle>(); maxSpeed = m_vehicle.maxSpeed; } public override Vector3 Force() { Vector3 tmpPos = new Vector3(transform.position.x, 0, transform.position.z); Vector3 tmpTargetPos = new Vector3(target.transform.position.x, 0, target.transform.position.z); //如果大于逃跑距离,那么返回向量0 if (Vector3.Distance(tmpPos,tmpTargetPos)>fearDistance) { return new Vector3(0, 0, 0); } //如果小于 计算 desiredVelocity = (transform.position - target.transform.position).normalized * maxSpeed; return (desiredVelocity - m_vehicle.velocity); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 抵达 /// </summary> public class SteeringForArrive : Steering { public bool isPanar = true; public float arrivalDistance = 0.3f; public float characterRadius = 1.2f; //当与目标小于这个距离时,开始减速 public float slowDownDistance; public GameObject target; private Vector3 desiredVelocity; private Vehicle m_vehicle; private float maxSpeed; void Start() { m_vehicle = GetComponent<Vehicle>(); maxSpeed = m_vehicle.maxSpeed; isPanar = m_vehicle.isPlanar; } public override Vector3 Force() { //角色与目标的距离 Vector3 toTarget = target.transform.position - transform.position; //预期速度 Vector3 desiredVelocity; Vector3 returnForce; if (isPanar) { toTarget.y = 0; } float distance = toTarget.magnitude; //大于减速半径 if (distance > slowDownDistance) { desiredVelocity = toTarget.normalized * maxSpeed; returnForce = desiredVelocity - m_vehicle.velocity; } else { desiredVelocity = toTarget - m_vehicle.velocity; returnForce = desiredVelocity - m_vehicle.velocity; } return returnForce; } void OnDrawGizmos() { //在目标范围画白色线框 Gizmos.DrawWireSphere(target.transform.position, slowDownDistance); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 追逐 /// </summary> public class SteeringForPursuit : Steering { public GameObject target; private Vector3 desiredVelocity; private Vehicle m_vehicle; private float maxSpeed; void Start() { m_vehicle = GetComponent<Vehicle>(); maxSpeed = m_vehicle.maxSpeed; } public override Vector3 Force() { Vector3 toTarget = target.transform.position - transform.position; //计算追逐者前向与逃避者前向的夹角 float relativeDirection = Vector3.Dot(transform.forward, target.transform.forward); //如果夹角大于0,且面对 if ((Vector3.Dot(toTarget,transform.forward)>0)&&(relativeDirection<-0.95f)) { //计算预期速度 desiredVelocity = (target.transform.position - transform.position).normalized * maxSpeed; //返回向量 return (desiredVelocity - m_vehicle.velocity); } //计算预测时间,距离/速度和 float lookaheadTime = toTarget.magnitude / (maxSpeed + target.GetComponent<Vehicle>().velocity.magnitude); //计算预期速度 desiredVelocity = (target.transform.position + target.GetComponent<Vehicle>().velocity * lookaheadTime - transform.position).normalized * maxSpeed; return (desiredVelocity - m_vehicle.velocity); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 逃避 /// </summary> public class SteeringForEvade : Steering { public GameObject target; private Vector3 desiredVelocity; private Vehicle m_vehicle; private float maxSpeed; void Start() { m_vehicle = GetComponent<Vehicle>(); maxSpeed = m_vehicle.maxSpeed; } public override Vector3 Force() { Vector3 toTarget = target.transform.position - transform.position; //预测时间 float lookaheadTime = toTarget.magnitude / (maxSpeed + target.GetComponent<Vehicle>().velocity.magnitude); //计算预期速度 desiredVelocity = (transform.position - (target.transform.position + target.GetComponent<Vehicle>().velocity * lookaheadTime)).normalized * maxSpeed; //返回操控向量 return (desiredVelocity - m_vehicle.velocity); } }