Unity Game Programming AI(4)壁障运动
来源:互联网 发布:python hmac sha1 编辑:程序博客网 时间:2024/04/29 17:02
Unity Game Programming AI(4)壁障运动
避开障碍物运动在游戏中也很常见,那样就离不开Raycast的使用。
关于Raycast的使用,可以参考:Raycast
下面我们就来实现这一功能:
using UnityEngine;using System.Collections;public class VehicleAvoidance : MonoBehaviour { public float speed = 20.0f; public float mass = 5.0f; public float force = 50.0f; public float minimumDistToAvoid = 20.0f; //Actual speed of the vehicle private float curSpeed; private Vector3 targetPoint;// Use this for initializationvoid Start () { mass = 5.0f; targetPoint = Vector3.zero;} void OnGUI() { GUILayout.Label("Click anywhere to move the vehicle to the clicked point"); }// Update is called once per framevoid Update () { //Vehicle move by mouse click RaycastHit hit; var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if(Input.GetMouseButtonDown(0) && Physics.Raycast(ray, out hit, 100.0f)) { targetPoint = hit.point; } //Directional vector to the target position Vector3 dir = (targetPoint - transform.position); dir.Normalize(); //Apply obstacle avoidance AvoidObstacles(ref dir); //Don't move the vehicle when the target point is reached if(Vector3.Distance(targetPoint, transform.position) < 3.0f) return; //Assign the speed with delta time curSpeed = speed * Time.deltaTime; //Rotate the vehicle to its target directional vector var rot = Quaternion.LookRotation(dir); transform.rotation = Quaternion.Slerp(transform.rotation, rot, 5.0f * Time.deltaTime); //Move the vehicle towards transform.position += transform.forward * curSpeed; } //Calculate the new directional vector to avoid the obstacle public void AvoidObstacles(ref Vector3 dir) { RaycastHit hit; //Only detect layer 8 (Obstacles) int layerMask = 1 << 8; //Check that the vehicle hit with the obstacles within it's minimum distance to avoid if (Physics.Raycast(transform.position, transform.forward, out hit, minimumDistToAvoid, layerMask)) { //Get the normal of the hit point to calculate the new direction Vector3 hitNormal = hit.normal; hitNormal.y = 0.0f; //Don't want to move in Y-Space //Get the new directional vector by adding force to vehicle's current forward vector dir = transform.forward + hitNormal * force; } }}
=================================================================================
结束。
0 0
- Unity Game Programming AI(4)壁障运动
- Unity Game Programming AI(1)人工智能导论
- Unity Game Programming AI(2)有限状态机
- Unity Game Programming AI(3)路径跟随
- Unity Game Programming AI(5)A Star寻路算法
- Unity Game Programming AI(6)行为树插件Behavior Designer
- Programming Game AI By Example
- Unity-AI(Sprite随机运动)
- [code] Programming Game AI by Example
- Learning Game AI Programming with lua(一)
- 书-Programming Game AI by Example
- Programming Game AI by Example ---Entity/EntityComponentSystem
- Unity人工智能学习—确定性AI算法之随机运动
- Unity人工智能确定性AI算法之随机运动
- game programming (4)
- 遗传算法入门(AI Techniques for Game Programming)
- Game Programming
- Game Programming Gems 4 by Andrew Kirmse
- BestCoder Round #54 (div.2) HDOJ 5429 Geometric Progression(模拟)
- iOS多线程--多线程简单概览
- 算法题:数组中只出现一次的两个数字
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
- Qt on Android: Hello World!
- Unity Game Programming AI(4)壁障运动
- ubuntu安装phpmyadmin
- POJ 3518 Prime Gap题意好难理解
- Ubuntu 14.04数据库服务器--mysql的安装和配置
- iOS网络--网络HTTP
- prlink elf format
- 归并排序
- ubuntu redis安装
- POJ2074Line of Sight【直线相交判定+求交点】