我使用统一资产第一人称控制器制作了一个游戏,允许玩家移动并让他们环顾四周。我把十字准线放在射线投射的地方并实例化一颗子弹。子弹射击在一定角度以上没有问题。子弹跟随十字线并在中心向右射击,但是如果我向下看太远,那么他们就不再向十字线发射的地方射击,而是直接射出相机。
我认为第一个人控制器制作的胶囊可能是问题,因为我在代码中找不到任何内容。
链接到视频: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;
}
}
}
答案 0 :(得分:1)
你可能是对的,它可能是来自控制器的胶囊问题,这样做: