私はユニティアセットのFirst Person Controllerを使用してプレイヤーの移動を許可し、周囲を見回すゲームを作成しました。私は、レイ・キャストが射撃して弾丸をインスタンス化するところに十字架をつけました。特定の角度以上の弾丸の発砲には何の問題もありません。弾丸は十字線に沿って中央で右に撃ちますが、私があまりにも遠く見下ろすと、もはや十字線がどこにあるかを撮影するのではなく、ちょうどカメラからまっすぐ撮影します。レイキャストが特定の角度を超えて応答していません
私のコードでは何も見つからないので、最初の人のコントローラが作るカプセルが問題になると思います。
VIDEO TO 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;
}
}
}
あなたのカーソル位置が間違っていると思われます。それが正しいことを確認しましたか? – Bart
どうすればいいですか? – ProgrammingLife