团结C#在敌人面前进行光线投射

团结C#在敌人面前进行光线投射,c#,unity3d,raycasting,C#,Unity3d,Raycasting,嗨,我有一个脚本应该在对象前面发送raycast。。。但是相反,它总是把raycast发送到同一个地方。。。不知道为什么 代码如下: //Send raycast to imitate enemy sight void SendRaycast() { //Start and end point of raycasts Vector3 startPosition = transform.position; Vector3 targetPosition = Vector3.z

嗨,我有一个脚本应该在对象前面发送raycast。。。但是相反,它总是把raycast发送到同一个地方。。。不知道为什么

代码如下:

//Send raycast to imitate enemy sight
void SendRaycast() {
    //Start and end point of raycasts
    Vector3 startPosition = transform.position;
    Vector3 targetPosition = Vector3.zero;

    //Start and end angle of raycasts
    int startAngle = (int)(-angle * 0.5F);  // half the angle to the Left of the forward
    int finishAngle = (int)(angle * 0.5F); // ..... and to the Right

    int inc = (int)( angle / 10 );

    //raycast that we are going to 'send'
    RaycastHit hit;

    // step through and find each target point
    for(int i = startAngle; i < finishAngle; i += inc ) // Angle from forward
    {
        targetPosition = (Quaternion.Euler( 0, i, 0 ) * transform.forward ).normalized * 5.0f;
        //targetPosition = transform.forward;

        // linecast between points
        if ( Physics.Linecast( startPosition, targetPosition, out hit ) )
        {
            if(hit.collider.gameObject.tag == "Player") {
                chase = true;
                Debug.Log("See Player");
            }
        }           
        // to show ray just for testing
        Debug.DrawLine( startPosition, targetPosition, Color.green );
    } 
}
//发送光线投射以模拟敌人的视线
void SendRaycast(){
//光线投射的起点和终点
Vector3起始位置=transform.position;
Vector3 targetPosition=Vector3.0;
//光线投射的开始和结束角度
int startAngle=(int)(-angle*0.5F);//前进方向左侧角度的一半
int finishAngle=(int)(角度*0.5F);/…并向右
int inc=(int)(角度/10);
//我们将要“发送”的raycast
雷卡斯特击中;
//逐步完成并找到每个目标点
对于(int i=startAngle;i
刚刚试用了您的代码,只要对象的局部z方向与世界Y方向正交,代码就可以正常工作。如果它们是共线的,则所有光线都重叠。你到底想达到什么目的?我对XZ有自上而下的看法,我希望敌人向前看,当他看到球员追他时:)但可能弄乱了一些东西,坐标在这里我添加了显示我问题的图像。。。堆栈上没有足够的rep:P