S
S
Sergey Klykov2015-06-20 09:59:01
C++ / C#
Sergey Klykov, 2015-06-20 09:59:01

UI Button button press, how to make a long press?

I created a 2D platformer control and put it on the UI Button so that when I press, for example, the right button, the character goes to the right and when I lower the button it stops, but somehow the button reacts to one click differently. That is, you need to repeatedly press the button so that the character goes to the right.

using UnityEngine;
using System.Collections;

public class characterController : MonoBehaviour {
        public float maxSpeed = 10f;
        public float jumpForce = 700f;
        bool facingRight = true;
        bool grounded;
        public Transform groundCheck;
        public float groundRadius = 0.2f;
        public LayerMask whatIsGround;
        public int score;
        float move = 0;

        // Use this for initialization
        void Start () {
                
        }
        
        // Update is called once per frame
        private void FixedUpdate () {
                #if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
                move = Input.GetAxis("Horizontal");
                #else
                Move (move);
                #endif
                grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        }

        void Update(){
                if ((Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown (KeyCode.UpArrow)) &&  grounded) {
                        
                        GetComponent<Rigidbody2D>().AddForce (new Vector2(0f,jumpForce));
                }
                GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
                
                if (move > 0 && !facingRight)
                        Flip ();
                else if (move < 0 && facingRight)
                        Flip ();
                
                
                
                if (Input.GetKey(KeyCode.Escape))
                {
                        Application.Quit();
                }
                
                if (Input.GetKey(KeyCode.R))
                {
                        Application.LoadLevel(Application.loadedLevel);
                }
                
                
        }

        void Move(float horizonalmove)
        {
                if (grounded) return;
                
                Vector3 moveVel = GetComponent<Rigidbody2D>().velocity;
                moveVel.x = horizonalmove * maxSpeed;
                GetComponent<Rigidbody2D>().velocity = moveVel;
        }

        public void Jump()
        {
                if (grounded) 
                        GetComponent<Rigidbody2D>().AddForce (new Vector2(0f,jumpForce));                         
        }

        public void StartMoving(float horizonalmove)
        {
                move = horizonalmove;
        }

        void Flip(){
                facingRight = !facingRight;
                Vector3 theScale = transform.localScale;
                theScale.x *= -1;
                transform.localScale = theScale;
        } 
        void OnGUI(){
                GUI.Box (new Rect (0, 0, 100, 100), "Stars: " + score);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2015-06-20
@DEMIGODDD

use getkey instead of keydown

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question