Laman

Unity Android OnGUI Controller

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    float maxSpeed = 10f;
    Animator anim;
    bool faceright = true;
    bool grounded = false;
    float groundRadius = 0.2f;
    public Transform groundCheck;
    public LayerMask whatIsGround;
    float jumpForce = 600f;
    float move = 0;
   
    //gui button
    public GUIStyle customButton;
    public Texture btnTexture1,btnTexture2,btnTexture3;
    private Rect posBtnJump =new Rect(Screen.width - 80,Screen.height -80, 70, 70);
    private Rect posBtnLeft =new Rect(10,Screen.height - 80, 70, 70);
    private Rect posBtnRight =new Rect(90,Screen.height - 80, 70, 70);
   
    // Use this for initialization
    void Start () {
        anim = GetComponent();
    }
   
    void FixedUpdate(){
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool("Ground",grounded);
        //if(!grounded)return;

        //using arrow on keyboard
        //move = Input.GetAxis("Horizontal");

        transform.Translate(new Vector2(move * maxSpeed * Time.deltaTime,0));
        //rigidbody2D.velocity = new Vector2(move * maxSpeed,rigidbody2D.velocity.y);
        anim.SetFloat("Speed", Mathf.Abs(move));
        //flip
        if(move > 0 && !faceright){flip();}else if(move < 0 && faceright){flip();}
       
    }

    //Flipping player
    void flip(){
        faceright = !faceright;
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }
   
    // Update is called once per frame
    void Update () {
        /* Jump using Space
         * if (Input.GetKeyDown(KeyCode.Space) && grounded){
            anim.SetBool("Ground",false);
            rigidbody2D.AddForce(new Vector2(0, jumpForce));
            grounded = false;
        }*/
        for (int i = 0; i < Input.touchCount; ++i){
            Touch touch = Input.GetTouch(i);
            // Get touch point and invert Y-axis.
            Vector2 touchPoint = touch.position;
            touchPoint.y = Screen.height - touchPoint.y;
            if (touch.phase == TouchPhase.Began){
                if (posBtnJump.Contains(touchPoint) && grounded){
                    anim.SetBool("Ground",false);
                    rigidbody2D.AddForce(new Vector2(0, jumpForce));
                    grounded = false;
                }
            }
            if (touch.phase == TouchPhase.Stationary){
                if (posBtnLeft.Contains(touchPoint)){
                    move= -1f;
                }else if (posBtnRight.Contains(touchPoint)){
                    move= 1f;
                }
            }else{
                move = 0;
            }
        }

        //Exit application
        if (Input.GetKeyUp(KeyCode.Escape)){
            Application.Quit();
            return;
        }
    }
   
    void OnGUI() {
        if (!btnTexture1 || !btnTexture2 || !btnTexture3) {
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }
       
        //left
        GUI.RepeatButton(posBtnLeft, btnTexture1, customButton);
        //right
        GUI.RepeatButton(posBtnRight, btnTexture2, customButton);
        //jump
        GUI.Button(posBtnJump, btnTexture3, customButton);
       
    }
}

No comments:

Post a Comment

Silahkan