2017-07-28 7 views
0

私はGoogle Chromeの恐竜のゲームを統一して編集しています。このゲームをモバイルで実行したいのですが、モバイルタッチを追加できませんでした。私はこのゲームのタッチ機能だけにしたい。画面に触れると、新しいゲームがジャンプまたは開始します。 (私はアヒルのオプションは必要ありません。)皆さんが私を助けてくれることを願っています。ユニティで恐竜のゲームにモバイルタッチを追加するには

using UnityEngine; 
using System.Collections; 

public class JumpAndDuck : MonoBehaviour 
{ 
    public Level level = null; 
    public GameObject ground = null; 
    public Collider2D standingCollider = null; 
    public Collider2D duckingCollider = null; 
    public AudioSource jumpAudioSource = null; 
    public AudioClip jumpAudioClip = null; 
    private Animator animator; 
    private bool grounded = true; 
    private bool ducking = false; 
    private float jumpVelocity = 0f; 
    private float gravity = 144f; 
    private Vector3 startVector; 

    void Start() 
    { 
     animator = GetComponent<Animator>(); 
     standingCollider.enabled = true; 
     duckingCollider.enabled = false; 
    } 

    void Update() 
    { 
     if (grounded) 
     { 
      if (Input.GetButton("Jump") || Input.GetAxis("Vertical") > 0) 
      { 
       jump(); 
      } 
      else if (Input.GetAxis("Vertical") < 0) 
      { 
       duck(); 
      } 
      else 
      { 
       stand(); 
      } 
     } 
     else 
     { 
      transform.position += jumpVelocity * Vector3.up * Time.deltaTime; 
      jumpVelocity -= gravity * Time.deltaTime; 

      if (transform.position.y < ground.transform.position.y) 
      { 
       grounded = true; 
       transform.position = startVector; 
       animator.SetBool("jumping", false); 
      } 
      else if (3 < transform.position.y && 20 < jumpVelocity) 
      { 
       jumpVelocity = 20; 
      } 
     } 
    } 

    void OnCollisionEnter2D(Collision2D collision) 
    { 
     if (collision.gameObject == ground) 
     { 
      grounded = true; 
      transform.position = startVector; 
      animator.SetBool("jumping", false); 
     } 
    } 

    void OnCollisionExit2D(Collision2D collision) 
    { 
     if (collision.gameObject == ground) 
     { 
      grounded = false; 
      animator.SetBool("jumping", true); 
     } 
    } 

    void jump() 
    { 
     if (!grounded) 
     { 
      return; 
     } 

     stand(); 
     if (jumpAudioSource && jumpAudioClip) 
     { 
      jumpAudioSource.PlayOneShot(jumpAudioClip, 1); 
     } 
     startVector = transform.position; 
     jumpVelocity = 40f + level.mainSpeed/10f; 
     grounded = false; 
     animator.SetBool("jumping", true); 
    } 

    void duck() 
    { 
     if (ducking || !grounded) 
     { 
      return; 
     } 

     standingCollider.enabled = false; 
     duckingCollider.enabled = true; 
     ducking = true; 
     animator.SetBool("ducking", true); 
    } 

    void stand() 
    { 
     if (!ducking) 
     { 
      return; 
     } 

     standingCollider.enabled = true; 
     duckingCollider.enabled = false; 
     ducking = false; 
     animator.SetBool("ducking", false); 
    } 
} 

コードとプロジェクトは私のものではありません。それはマウスのクリックでPC上でうまくいきます、私はタッチ機能付き携帯でそれを行う必要があります。

+0

Input.GetMouseButtonDown(0)、タッチのために使用することができます。私は来ることを意味する。私はGoogleで、私の時間の30秒を見つけた。 – Thalthanas

+0

[this](https://docs.unity3d.com/460/Documentation/ScriptReference/Input.GetTouch.html)のようなものでしょうか? –

+0

ありがとう、私は今それを修正する。あなたは正しいです、これはとても基本的ですが、私はここで新しいです。 –

答えて

0

あなたは状態は、各フレームをリセットし得ることから、Update関数からこの関数を呼び出す必要があり、このコード

using UnityEngine; 
using System.Collections; 

public class ExampleClass : MonoBehaviour { 
    void Update() { 
     if (Input.GetMouseButtonDown(0)) 
      Debug.Log("Pressed left click."); 

     if (Input.GetMouseButtonDown(1)) 
      Debug.Log("Pressed right click."); 

     if (Input.GetMouseButtonDown(2)) 
      Debug.Log("Pressed middle click."); 

    } 
} 

を使用することができます。ユーザーがマウスボタンを離してもう一度押すまでtrueに戻りません。ボタンの値は、左ボタンの場合は0、右ボタンの場合は1、中ボタンの場合は2です。

URL:https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html

関連する問題