Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 光线投射在某个角度后不响应_C#_Unity3d_Unity5_Game Physics_Raycasting - Fatal编程技术网

C# 光线投射在某个角度后不响应

C# 光线投射在某个角度后不响应,c#,unity3d,unity5,game-physics,raycasting,C#,Unity3d,Unity5,Game Physics,Raycasting,我用unity asset第一人称控制器制作了一个游戏,允许玩家移动,让他们四处看看。我在光线投射穿过并实例化子弹的地方放了一根十字线。子弹射出一定角度以上没有问题。子弹沿着十字线从中间射出,但如果我向下看得太远,它们就不会在十字线所在的地方射出,而是直接从相机中射出 我认为第一人称控制器制造的胶囊可能是问题所在,因为我在代码中找不到任何东西 视频链接: Bullet Listener.cs using System.Collections; using System.Collections.G

我用unity asset第一人称控制器制作了一个游戏,允许玩家移动,让他们四处看看。我在光线投射穿过并实例化子弹的地方放了一根十字线。子弹射出一定角度以上没有问题。子弹沿着十字线从中间射出,但如果我向下看得太远,它们就不会在十字线所在的地方射出,而是直接从相机中射出

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

视频链接:

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 ();
    }
}
}
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;
    }
}  
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类:单一行为{
公共摄像机;
公共公报控制公报预制件;
公共游戏对象光标;
专用矢量3光标位置;
无效更新(){
if(Input.GetMouseButtonDown(0)){
cursorPosition=cursor.transform.position;
//从摄影机到鼠标位置创建光线
Ray Ray=Main Camera.Screen PointToRay(光标位置);
//从预置创建项目符号
BulletContoller-newBullet=实例化(bulletPrefab.gameObject).GetComponent();
//让新的子弹从摄像机开始
newBullet.transform.position=main camera.transform.position;
//设定子弹方向
newBullet.SetDirection(射线方向);
//制造子弹声
AudioSource audio=GetComponent();
audio.Play();
}
}
}
子弹控制器.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 ();
    }
}
}
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;
    }
}  
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类BulletController:单行为{
刚体rb;
公共安全部队;
bool firstTime=false;
矢量3方向;
//用于初始化
无效开始(){
rb=GetComponent();
}
公共无效设置方向(矢量3方向){
方向=方向;
第一次=正确;
}
无效碰撞中心(碰撞柱){
//子弹击中某物时的代码
如果(col.gameObject.name==“Target”){
this.gameObject.name=“Hit”;
}
}
无效固定更新(){
如果(第一次){
rb.AddForce(方向*子弹力);
第一次=错误;
}
}  
}

您可能是对的,这可能是来自控制器的胶囊问题,请执行以下操作:

  • 创建两个名为Player子弹的图层
  • 将PlayerController放置在层播放器
  • 将项目符号放置在层项目符号中
  • 转到编辑->项目设置->物理-并在层碰撞矩阵中确保播放器层和子弹层不会相互碰撞

  • 我怀疑是你的位置错了。你确认它是正确的吗?我该怎么做?现在可以了。谢谢你的帮助。我以前不知道