2017-12-16 26 views
0

私はユニティアセットの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; 
    } 
} 
} 
+0

あなたのカーソル位置が間違っていると思われます。それが正しいことを確認しましたか? – Bart

+0

どうすればいいですか? – ProgrammingLife

答えて

1

あなたが正しいかもしれない、それはすることができますコントローラからのカプセル問題は、次の操作を行います

  1. プレーヤー箇条書きという名前の2層を作成します。
  2. PlayerControllerをレイヤーのプレーヤーに配置します。
  3. レイヤーに箇条書きを置きます。箇条書き。 >プロジェクトの設定 - - 編集に
  4. ゴー>物理学とレイヤ衝突マトリックスでは、プレーヤー層と箇条書き層が互いに衝突しないことを確認してください。
+0

現在動作中です。助けてくれてありがとう。前にそれについて知りませんでした – ProgrammingLife

関連する問題