射线(Ray)是几乎所有3D游戏都会使用的一项技术,被广泛地运用到路径搜寻、障碍判断等游戏操作中。
例如:自动巡逻的机器人在发现视野前方有敌人的时候会自动进行攻击,运动的汽车发现前方有障碍物会停止运动。
************************************************************************************************************************************
1.固定方向移动与障碍判断
游戏物体自动向指定的一个方向移动,当侦测到前方有障碍的时候停止移动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour {
private GameObject obstacle; //通过find来发现指定名称的GameObject
private Vector3 direction = new Vector3(1, 0, 0); //x轴正方向,这里的数值直接影响着移动方向与移动的单位长度
private float distance = 3f; //射线的长度
public float move_speed = 2f; //角色自动移动的速度
bool Judgemove = false; //物体停止时候的判断条件
// Use this for initialization
void Start () {
obstacle = GameObject.Find("Cube");
}
// Update is called once per frame
void Upd