i have a ball, and 3 box, this ball will be player, i give this ball bouncy and rigidbody, and the ball will continue to jump,, than, i use GUI Button to control the ball to the right, and left, from the box to another box,, i use this script to do that, Code (CSharp): public float speed = 180; public void RightButton() { rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(speed,0) * Time.deltaTime); } public void LeftButton() { rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(-speed,0) * Time.deltaTime); } but user still can spam with this script, i want the ball can only move once per jump, i need the ball grounded once time before it move again, so player can feel the rhythm of the games, so how to do that? i didnt have any programing skill,,
Try something like this: Code (CSharp): using UnityEngine; using System.Collections; public class Bounce : MonoBehaviour { public float speed = 180; private bool grounded = false; float rayDistance = 0.2f; private void Update() { RaycastHit2D rh = Physics2D.Raycast(transform.position, -Vector2.up, rayDistance); if (rh.collider != null && rh.collider.gameObject.tag == "Ground") { grounded = true; } } public void RightButton() { if (grounded) { rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(speed, 0) * Time.deltaTime); } } public void LeftButton() { if (grounded) { rigidbody2D.MovePosition(rigidbody2D.position + new Vector2(-speed, 0) * Time.deltaTime); } } }