光线投射没有超过某个角度

问题描述 投票:0回答:1

我使用统一资产第一人称控制器制作了一个游戏,允许玩家移动并让他们环顾四周。我把十字准线放在射线投射的地方并实例化一颗子弹。子弹射击在一定角度以上没有问题。子弹跟随十字线并在中心向右射击,但是如果我向下看太远,那么他们就不再向十字线发射的地方射击,而是直接射出相机。

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

链接到视频:https://youtu.be/zf2EuL7e_i4

Bullet 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 ();
    }
}
}

Bullet 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;
    }
}  
}
c# unity3d unity5 game-physics raycasting
1个回答
1
投票

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

  1. 创建2个名为Player和Bullets的图层。
  2. 将PlayerController放在图层Player中。
  3. 将子弹放在图章项目符号中。
  4. 转到编辑 - >项目设置 - >物理,在图层碰撞矩阵中,确保播放器图层和项目符号图层不会相互冲突。
© www.soinside.com 2019 - 2024. All rights reserved.