Pressing Vs Jumping
Hello, I'm still fairly new to unity and following tutorials in order to learn/improve. In one of the tutorials, a script was made in order to change jump height (higher or lower) depending on if the user pressed or held down the button. It works if I use the space bar, but when I use the touch screen on my computer or click with the mouse it does the same jump height each time. How would one make this work for touch screen ex. for a mobile game where you might not have access to buttons.I thought it would be easier if you can see my script so I added it. Also i was thinking that if i can't figure out the issue with the touchscreen then i would create a button that basically acted like a space bar for when the game is played on a mobile device.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;
public class PlayerContol : MonoBehaviour { public float moveSpeed; private float moveSpeedStore; public float speedMultiplier;
public float milestone;
private float speedIncreaseMileStoneStore;
private float milestoneCount;
private float speedMilestoneCountStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCount;
private bool stoppedJumping;
private bool canDoubleJump;
private Rigidbody2D myRigidbody;
public bool grounded;
//private bool isFalling;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRad;
public GameManger theGameManager;
//private Collider2D myCollider;
private Animator myAnimator;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
//myCollider = GetComponent<Collider2D>();
jumpTimeCount = jumpTime;
milestoneCount = milestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = milestoneCount;
speedIncreaseMileStoneStore = milestone;
stoppedJumping = true;
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
//grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRad, whatIsGround);
if(transform.position.x > milestoneCount)
{
milestoneCount += milestone;
milestone = milestone * speedMultiplier;
moveSpeed = moveSpeed * speedMultiplier;
speedMilestoneCountStore = milestone;
}
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if /*(Input.GetButtonDown("Jump"))*/ (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
//prevents jump on pause
if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
return;
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
return;
}
//jump code
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;//false
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCount = jumpTime;
stoppedJumping = false;
canDoubleJump = false;//false
}
}
if/*(Input.GetButtonDown("Jump"))*/((Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0)) && !stoppedJumping)
{
//prevents jump on pause
if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
return;
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
return;
}
//jump code
if (jumpTimeCount > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCount -= Time.deltaTime;
}
}
if /*(Input.GetButtonUp("Jump"))*/(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0) )
{
//prevents jump on pause
if (EventSystem.current.IsPointerOverGameObject() && EventSystem.current.currentSelectedGameObject == null)
return;
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
return;
}
//jump code
jumpTimeCount = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCount = jumpTime;
canDoubleJump = true;
}
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("Grounded", grounded);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "KillBox")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
milestoneCount = speedMilestoneCountStore;
milestone = speedIncreaseMileStoneStore;
}
}
}
Answer by KevRev · Apr 16, 2019 at 07:37 PM
GetMouseButtonDown triggers once on click, but GetMouseButton is true whilst the button is held.
In the first if check, try:
Input.GetMouseButton(0)
Then instead of using Input.GetMouseButtonUp(0):
If (!Input.GetMouseButton(0))