- Home /
Question by
Gramadi · Nov 24, 2014 at 09:35 AM ·
androidjumpplatformer
Problem with touch button on android platformer game.
Hello everyone, iam creating platformer game which has 3 button. one button to move left, one button to move right and one button to move right.
Here is my sample code for moveScript and JumpScript.
using UnityEngine;
using System.Collections;
public class JumpAlma : MonoBehaviour {
public bool jump = false;
public float jumpForce = 450;
public GameObject alma;
public Transform groundCheck;
private bool grounded = false;
public bool player1 = true;
public AudioClip audio ;
public Animator animator;
void Awake () {
groundCheck = alma.gameObject.transform.Find("groundCheck");
}
void Update () {
grounded = Physics2D.Linecast(alma.gameObject.transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
if (jump && grounded) {
// animator.SetBool("Jump",true);
AudioSource.PlayClipAtPoint(audio,transform.position);
alma.gameObject.rigidbody2D.AddForce (new Vector2 (0f, jumpForce));
StartCoroutine(delayJump());
jump = false;
}
}
IEnumerator delayJump(){
yield return new WaitForSeconds (0.8f);
// animator.SetBool ("Jump", false);
}
void OnMouseDown(){
gameObject.transform.localScale = new Vector3(0.8f, 0.8f, 0);
}
void OnMouseUp() {
Debug.Log (grounded);
gameObject.transform.localScale = new Vector3(1f, 1f, 0);
jump = true;
}
}
using UnityEngine;
using System.Collections;
public class Walk : MonoBehaviour {
public GameObject alma;
public Transform groundCheck;
private bool grounded = false;
public bool player1 = true;
public AudioClip audio ;
public Animator animator;
public bool left;
public float movementSpeed;
void Awake () {
groundCheck = alma.gameObject.transform.Find("groundCheck");
}
void Update () {
grounded = Physics2D.Linecast(alma.gameObject.transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
}
void OnMouseDrag(){
if (left) {
movementSpeed = -3f;
}
else {
movementSpeed = 3f;
}
if(grounded){
alma.gameObject.rigidbody2D.velocity = new Vector3 (movementSpeed, 0, 0);
}
gameObject.transform.localScale = new Vector3(0.8f, 0.8f, 0);
}
void OnMouseUp() {
gameObject.transform.localScale = new Vector3(1f, 1f, 0);
}
}
Move Button work well , Jump Button too.. The problem is when i hold move button then i press jump button it wont jump.
anyone can help me :) , thx before
Comment
Your answer
Follow this Question
Related Questions
How to fix Jump problem with 2d Platformer Unity demo game? 0 Answers
Player stucken in the wall while jumping 2D 1 Answer
Network Client does not Groundcheck 0 Answers
android Bluetooth multiplayer 1 Answer
Jump Not Working 1 Answer