您的位置:首页 > 移动开发 > Unity3D

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

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  人工智能 unity3d