Question by
Cresent_ · Dec 26, 2016 at 09:21 PM ·
androidtouchtouch controlstouchscreenendless runner
When The Screen Is Touched Jump
Hey all Im new to game development and want to make a mobile game. Whats the most simple way to make the sprite jump when the screen is touched. Im following a tutorial for guidance but its using PC inputs not mobile. Below is what I have, also for some reason not all the var's etc that turn a different colour on the tutorial dont on mine and im doing the same as him whats happening! Im using keyboard clicks atm until i can get a solution to the screen tapping.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float jumpForce;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
private Collider2D myCollider;
private Animator myAnimator;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D>();
myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
myRigidbody.velocity = new Vector2 (moveSpeed, myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space))
{
if (grounded)
{
myRigidbody.velocity = new Vector2 (myRigidbody.velocity.x, jumpForce);
}
}
myAnimator.SetFloat ("Speed", myRigidbody.velocity.x);
myAnimator.SetBool ("Grounded", grounded);
}
}
Comment