光线投射在一定角度后没有响应

raycast not responding past a certain angle

我使用统一资产第一人称控制器制作了一款游戏,允许玩家移动并让他们环顾四周。我在光线投射穿过并实例化子弹的地方放了一个十字线。一定角度以上的子弹射击是没有问题的。子弹沿着十字准线射向正中,但如果我往下看太远,它们就不再射击十字准线所在的位置,而是直接从相机中射出。

我认为第一人称控制器制作的胶囊可能是问题所在,因为我在我的代码中找不到任何内容。

LINK 视频:https://youtu.be/zf2EuL7e_i4

子弹Listener.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletListener : MonoBehaviour {
public Camera mainCamera;
public BulletContoller bulletPrefab;
public GameObject cursor;
private Vector3 cursorPosition;

void Update () {
    if (Input.GetMouseButtonDown (0)) {

        cursorPosition = cursor.transform.position;

        //create ray from camera to mousePosition
        Ray ray = mainCamera.ScreenPointToRay (cursorPosition);

        //Create bullet from the prefab
        BulletContoller newBullet = Instantiate (bulletPrefab.gameObject).GetComponent<BulletContoller> ();

        //Make the new bullet start at camera
        newBullet.transform.position = mainCamera.transform.position;

        //set bullet direction
        newBullet.SetDirection (ray.direction);

        //Create Bullet Sound
        AudioSource audio = GetComponent<AudioSource>();

        audio.Play ();
    }
}
}

子弹Controller.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletContoller : MonoBehaviour {

Rigidbody rb;
public float bulletForce;
bool firstTime = false;
Vector3 direction;

// Use this for initialization
void Start () {
    rb = GetComponent<Rigidbody> ();
}


public void SetDirection (Vector3 dir) {
    direction = dir;
    firstTime = true;
}

void OnCollisionEnter (Collision col) {
    //code for when bullet hits something
    if (col.gameObject.name == "Target") {
        this.gameObject.name = "Hit";
    }
}

void FixedUpdate () {
    if (firstTime) {
        rb.AddForce (direction * bulletForce);
        firstTime = false;
    }
}  
}

你可能是对的,可能是控制器的胶囊问题,这样做:

  1. 创建 2 个图层,分别命名为 PlayerBullets.
  2. 将PlayerController放在层Player.
  3. 将 Bullet 放在图层 Bullets.
  4. 转到 编辑 -> 项目设置 -> 物理 并在 层碰撞矩阵 中确保 Player 层和 Bullets 层不会相互碰撞。