所以我设法修改了一些我为光线投射找到的代码,以便我的敌人向我的第一人称相机的坐标移动。但是,我需要改变光线投射在场景中的障碍物周围移动敌人的方式。我对myTransform
//变量感到有些困惑transform
。target
所以这是我的代码;
var speed : float = 10.0;
private var dir : Vector3;
private var dirFull : Vector3;
var target : Transform;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake()
{
myTransform = transform;
}
function Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform; //Target the player
}
function FixedUpdate()
{
// the directional vector to the target
dir = (target - transform.position).normalized;
var hit : RaycastHit;
// more raycasts
var leftRay = transform.position + Vector3(-0.125, 0, 0);
var rightRay = transform.position + Vector3(0.125, 0, 0);
// check for forward raycast
if (Physics.Raycast(transform.position, transform.forward, hit, 5)) /
{
if (hit.transform != this.transform)
{
Debug.DrawLine (transform.position, hit.point, Color.white);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// check for leftRay raycast
if (Physics.Raycast(leftRay, transform.forward, hit, 5))
{
if (hit.transform != this.transform)
{
Debug.DrawLine (leftRay, hit.point, Color.red);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// check for rightRay raycast
if (Physics.Raycast(rightRay, transform.forward, hit, 5))
{
if (hit.transform != this.transform)
{
Debug.DrawLine (rightRay, hit.point, Color.green);
dir += hit.normal * 20; // 20 is force to repel by
}
}
// rotation
var rot = Quaternion.LookRotation (dir);
transform.rotation = Quaternion.Slerp (transform.rotation, rot, Time.deltaTime);
//position
transform.position += transform.forward * (2 * Time.deltaTime); // 20 is speed
}
我几乎可以肯定我唯一需要更改的是dir
变量,以便它使用新的转换代码。但正如我所说,我一直对使用哪个变量感到困惑。
编辑:我的理解是我需要更改更新底部的代码,以便考虑到通过光线投射遇到障碍物时发生的旋转。这就是为什么我认为只有“dir”和“rot”需要更改,然后在位置和旋转代码中使用。