Skip to main content

比较sb的自动寻路 只是单层的 ,不撞墙不掉坑。

var q1:Transform;
var q2:Transform;
var d1:Transform;
var d2:Transform;
static var bian:int;
static var newpoint:Vector3;
function Start (){
newpoint=transform.position;
}
function Update () {
if(q1==null||q2==null||d1==null){return;}
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
if(Input.GetKeyDown(KeyCode.Mouse0)){
newpoint =hit.point;
transform.LookAt(Vector3(newpoint[0],transform.position.y,newpoint[2]));
}
}

bian++;
if(bian==200){bian=0;transform.LookAt(Vector3(newpoint[0],transform.position.y,newpoint[2])); }


if(Vector3.Distance(transform.position,newpoint)>2)
{
var fwd = transform.TransformDirection (Vector3.forward);
var fwd1 = transform.TransformDirection (Vector3.up);
if(Physics.Raycast(q1.transform.position, fwd, 1)||Physics.Raycast(q2.transform.position, fwd, 1)||Physics.Raycast(d1.transform.position, -fwd1, 0.3)==false||Physics.Raycast(d2.transform.position, -fwd1, 0.3)==false)
{transform.Rotate(Vector3.up *45*Time.deltaTime, Space.Self);}

if(Physics.Raycast(q1.transform.position, fwd, 1)==false&&Physics.Raycast(q2.transform.position, fwd, 1)==false&&Physics.Raycast(d1.transform.position, -fwd1, 0.3)&&Physics.Raycast(d2.transform.position, -fwd1, 0.3))
{transform.Translate(Vector3.forward *2* Time.deltaTime, Space.Self);}
}
//~ print(Mathf.Round(Random.value*45)+45);
}