2016-06-11 5 views
0

Unityでは、Update()関数とFire()関数のInput.GetMouseButton(0)が動いているときに呼び出されないという、本当に面倒な問題があります。画面の周りの私のプレーヤー。プレイヤーが停止していて、マウスを押さえていると、それは真を返し、後に私が持っているコードが呼び出されます。Unity/C# - Input.GetMouseButtonが呼び出されていない

私が停止中に発射し、発射を続けながら移動を開始すると、発砲を中止したり、移動できなくなります。

代わりにif (Input.GetAxis("Fire1") !=0)を使ってみましたが、変更はありませんでした。

void Update() 
{ 
    if (Input.GetMouseButton(0)) 
     Debug.Log("Mouse Pressed"); // Not called when player is moving 

    mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); 

    Fire(m_gunType); 

    PerformRotation(); 

    PerformMovement(); 
} 

private void PerformMovement() 
{ 
    if (Input.GetKey(KeyCode.W)) 
     m_rb.AddForce(new Vector2(0, m_speed * Time.deltaTime)); 
    if (Input.GetKey(KeyCode.A)) 
     m_rb.AddForce(new Vector2(-m_speed * Time.deltaTime, 0)); 
    if (Input.GetKey(KeyCode.S)) 
     m_rb.AddForce(new Vector2(0, -m_speed * Time.deltaTime)); 
    if (Input.GetKey(KeyCode.D)) 
     m_rb.AddForce(new Vector2(m_speed * Time.deltaTime, 0)); 
} 

private void Fire(GunType gt) 
{ 
    Debug.Log("Fire called"); // Called every frame 

    if (Input.GetMouseButton(0)) 
    { 
     Debug.Log("Fired"); // Only called when player is stationary 

     // Spawn bullet code 
    } 
} 


private void PerformRotation() 
{ 
    // if m_firePointOffset if changed in the inspector 
    if (m_firePoint.localPosition != (Vector3)m_firePointOffset) 
     m_firePoint.localPosition = (Vector3)m_firePointOffset; 

    // Get Angle in Radians 
    float AngleRad = Mathf.Atan2(mouseScreenPosition.y - transform.position.y, mouseScreenPosition.x - transform.position.x); 
    // Get Angle in Degrees 
    float AngleDeg = (180/Mathf.PI) * AngleRad; 
    // Rotate Object 
    this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg); 
} 
+1

問題はありません。これは完全なコードですか?そうでない場合は、多分投稿してください。また、必要なコンポーネントをすべて取り出し、それらを新しいプロジェクトやシーンに入れてテストします。しばしば問題を引き起こしている無関係のものです。しかし、具体的には、GetMouseButtonメソッド自体がUpdateからポーリングされても問題を引き起こすことはありません。 – Xarbrough

+0

変数と、スクリプトの唯一のコードであるPerformRotation()とは別に、私は新しいプロジェクトで再作成し、それが起こったばかりのものであることを願っています。 –

+0

@ MarkInnes私はあなたの 'PerformRotation()'コードを実際に見たいと思っています。可能であれば、新しいシーンではなく新しいプロジェクトでこのコードを実行してみてください。 – Programmer

答えて

0

私はGetKeyDownで私のGetMouseButtonGetKeyを交換し、特別にあなたがキーを離したときに前に設定され、すべての入力をキャンセルし無効とされなかったものを、あまりにもこの問題を持っていた、そしてvoid Update() にこれを追加しました例えば、

private void PerformMovement() 
{ 
if (Input.GetKeyDown(KeyCode.W)) 
    m_rb.AddForce(new Vector2(0, m_speed * Time.deltaTime)); 
} 
private void CancelFunction() 
{ 
if (Input.GetKeyUp(KeyCode.W)) 
    m_rb.velocity = 0; 
} 

は、その後、他のすべてのキーとCancelFunctionを埋めることができ、あなたの更新に追加します。それはちょっとこれに "荒い道"ですが、それがうまくいくかどうか試してみてください。

関連する問題