01 Unity3D人工智能AI-靠近
2016-10-22 14:43
302 查看
本文固定连接:http://blog.csdn.net/u013108312/article/details/52892210
1.新建场景
2.创建一个目标点
3.添加角色,添加Character Controller,AILocomotion.cs和SteeringForSeek.cs
1.新建场景
2.创建一个目标点
3.添加角色,添加Character Controller,AILocomotion.cs和SteeringForSeek.cs
using UnityEngine; using System.Collections; public abstract class Steering : MonoBehaviour { public float weight = 1; public virtual Vector3 Force() { return new Vector3(0,0,0); } }
using UnityEngine; using System.Collections; //using System.Collections.Generic; public class Vehicle : MonoBehaviour { private Steering[] steerings; public float maxSpeed = 10; public float maxForce = 100; protected float sqrMaxSpeed; public float mass = 1; public Vector3 velocity; public float damping = 0.9f; public float computeInterval = 0.2f; public bool isPlanar = true; private Vector3 steeringForce; protected Vector3 acceleration; //private CharacterController controller; //private Rigidbody theRigidbody; //private Vector3 moveDistance; private float timer; protected void Start () { steeringForce = new Vector3(0,0,0); sqrMaxSpeed = maxSpeed * maxSpeed; //moveDistance = new Vector3(0,0,0); timer = 0; steerings = GetComponents<Steering>(); //controller = GetComponent<CharacterController>(); //theRigidbody = GetComponent<Rigidbody>(); } void Update () { timer += Time.deltaTime; steeringForce = new Vector3(0,0,0); //ticked part, we will not compute force every frame if (timer > computeInterval) { foreach (Steering s in steerings) { if (s.enabled) steeringForce += s.Force()*s.weight; } steeringForce = Vector3.ClampMagnitude(steeringForce,maxForce); acceleration = steeringForce / mass; timer = 0; } } /* void FixedUpdate() { velocity += acceleration * Time.fixedDeltaTime; if (velocity.sqrMagnitude > sqrMaxSpeed) velocity = velocity.normalized * maxSpeed; moveDistance = velocity * Time.fixedDeltaTime; if (isPlanar) moveDistance.y = 0; if (controller != null) controller.SimpleMove(velocity); else if (theRigidbody == null || theRigidbody.isKinematic) transform.position += moveDistance; else theRigidbody.MovePosition(theRigidbody.position + moveDistance); //updata facing direction if (velocity.sqrMagnitude > 0.00001) { Vector3 newForward = Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime); newForward.y = 0; transform.forward = newForward; } }*/ }
using UnityEngine; using System.Collections; public class AILocomotion : Vehicle { private CharacterController controller; private Rigidbody theRigidbody; private Vector3 moveDistance; public bool displayTrack; // Use this for initialization void Start () { controller = GetComponent<CharacterController>(); theRigidbody = GetComponent<Rigidbody>(); moveDistance = new Vector3(0,0,0); base.Start(); } void FixedUpdate() { velocity += acceleration * Time.fixedDeltaTime; if (velocity.sqrMagnitude > sqrMaxSpeed) velocity = velocity.normalized * maxSpeed; moveDistance = velocity * Time.fixedDeltaTime; if (isPlanar) { velocity.y = 0; moveDistance.y = 0; } if (displayTrack) //Debug.DrawLine(transform.position, transform.position + moveDistance, Color.red,30.0f); Debug.DrawLine(transform.position, transform.position + moveDistance, Color.black, 30.0f); if (controller != null) { //if (displayTrack) //Debug.DrawLine(transform.position, transform.position + moveDistance, Color.blue,20.0f); controller.SimpleMove(velocity); } else if (theRigidbody == null || theRigidbody.isKinematic) { transform.position += moveDistance; } else { theRigidbody.MovePosition(theRigidbody.position + moveDistance); } //updata facing direction if (velocity.sqrMagnitude > 0.00001) { Vector3 newForward = Vector3.Slerp(transform.forward, velocity, damping * Time.deltaTime); if (isPlanar) newForward.y = 0; transform.forward = newForward; } //gameObject.animation.Play("walk"); } }
using UnityEngine; using System.Collections; public class SteeringForSeek : Steering { public GameObject target; private Vector3 desiredVelocity; private Vehicle m_vehicle; private float maxSpeed; 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); } }
相关文章推荐
- 01我为什么学Unity3d
- 08 Unity3D人工智能AI-避开障碍
- Unity3d 学习01
- unity3D与Socket通信_01
- Unity3D手机斗地主游戏开发实战(01)_发牌功能实现
- 【Unity3D / Extension】 扩展方法01 —— 扩展Transfrom
- 我的世界 Unity3D MineCraft 用Unity3D制作类似MineCraft我的世界的游戏 正经梳理一下开发01
- 01我为什么学Unity3d
- Unity3D 01-各种寻找GameObject方法
- Unity3D-Day_01
- 【Unity3D】学习笔记(第1记) 敌人AI之Seek(靠近)
- Unity3D实践系列01,创建项目
- 01我为什么学Unity3d
- 02 Unity3D人工智能AI-离开
- unity3d角色控制器01
- Unity3D之结合TexturePacker使用显示贴图Part01
- 03 Unity3D人工智能AI-抵达
- 01_Unity3D的输入(Input)——键盘和鼠标
- unity3d教程-01-安装及使用Unity
- 04 Unity3D人工智能AI-追逐