So I’m rather new to Unity and am trying to create a minimalist platformer. I’m using a sphere for the player and it moves on cubes. I’m using the following script for the player controller. I would like to only allow the moveUp method to happen if the sphere is currently colliding with a cube allowing me enforce a single jump at a time. Is there an easy of concise way to enforce this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public GameObject player;
public float movespeed;
public float upforce;
private Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = player.GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.W)) {
moveUp ();
}else if (Input.GetKey (KeyCode.A)) {
moveLeft();
}else if (Input.GetKey (KeyCode.D)) {
moveRight();
}
}
public void moveUp(){
rb.AddForce(new Vector3(0, upforce, 0));
}
public void moveRight(){
rb.AddForce(new Vector3(movespeed, 0, 0));
}
public void moveLeft(){
rb.AddForce(new Vector3(-movespeed, 0, 0));
}
}