Unity C#在敌人面前进行Raycasting

时间:2014-05-10 13:54:24

标签: c# unity3d raycasting

嗨我有脚本应该在对象面前发送光线投射......但是它会将光线投射总是发送到同一个地方......不知道为什么

这是代码:

//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 );
    } 
}

0 个答案:

没有答案