我试图让敌人跟随我的玩家,如果他们靠近他。 如果他直接在他们面前,我让他们跟着我的球员,但这不是我想要的。我希望他们通过光线投射他的位置或某种方式让他们在他们如此接近时跟随他。
这是我需要帮助的一点。它附着在敌人的预制件上,并被实例化。
var playerray : RaycastHit;
var fwd = transform.TransformDirection (Vector3(Player.transform.position.x,Player.transform.position.y,Player.transform.position.z));
if (Physics.Raycast(myTransform.position,fwd, playerray, DetectionDist)){
Debug.DrawRay (transform.position, fwd, Color.red);
}
基本上由于某种原因,方向已关闭,我无法弄清楚原因。
以下是完整的脚本以防万一。
#pragma strict
var Speed : float = 8000;
var RayDist : float = 100;
var speed : float = 100;
var Player : Transform;
var attackPoint : Transform;
var Bullet : GameObject;
var myTransform : Transform;
var attackbase: boolean;
var spotted :boolean;
var DetectionDist : int = 100;
function Start(){
myTransform = transform;
Player = GameObject.Find("Player").transform;
attackPoint = GameObject.Find("attackPoint").transform;
}
function Update () {
var hit: RaycastHit;
if (Physics.Raycast(transform.position,transform.forward, hit, RayDist)){
Debug.DrawRay(transform.position,Vector3.forward, Color.green);
if(hit.collider.tag == "Player")
{
spotted = true;
}
}
if(spotted == true){
var F = Quaternion.LookRotation(Vector3(Player.position.x, myTransform.position.y, Player.position.z) - myTransform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, F, speed * Time.deltaTime);
myTransform.position += myTransform.forward * Speed * Time.deltaTime;
}
if(spotted == false){
var A = Quaternion.LookRotation(Vector3(attackPoint.position.x, myTransform.position.y, attackPoint.position.z) - myTransform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, A, speed * Time.deltaTime);
myTransform.position += myTransform.forward * Speed * Time.deltaTime;
}
//////////////////////ENEMY TO PLAYER RAY////////////////////////////////////////
var playerray : RaycastHit;
var fwd = transform.TransformDirection (Vector3(Player.transform.position.x,Player.transform.position.y,Player.transform.position.z));
if (Physics.Raycast(myTransform.position,fwd, playerray, DetectionDist)){
Debug.DrawRay (transform.position, fwd, Color.red);
}
}
function OnCollisionEnter(coll : Collision){
if(coll.gameObject.tag == "Pbullet"){
Destroy(gameObject);
}
if(coll.gameObject.tag == "Player"){
///attack and make Player flash red.
}
}
答案 0 :(得分:0)
var fwd = (Player.transform.position - transform.position).normalized;
此代码修复了我的错误。它现在是一个完美的光线。