• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

Unity3D人工智能AI-靠近

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1120/199.html

Unity3D人工智能AI-靠近

 

目前AI已经渗透到人们的日常生活当中,对于开发人员来说要解决的是如何用AI延长你的游戏的生命周期,让你的游戏更加具有挑战性,下面我们常州微信小程序开发专家-幻天网络要给大家分析的是人工智能AI开发中的靠近功能,让AI自动靠近物体。

 

具体实现步骤如下:

 

1.新建场景  

2.创建一个目标点  

3.添加角色,添加CharacterController,AILocomotion.csSteeringForSeek.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);

   



上篇:上一篇:c#json操作
下篇:下一篇:Unity3D人工智能AI-随机徘徊