Skip to content

Instantly share code, notes, and snippets.

@brismithSFHS
Last active April 19, 2024 21:21
Show Gist options
  • Save brismithSFHS/109f11ea8afe09242451f5c1575f3be3 to your computer and use it in GitHub Desktop.
Save brismithSFHS/109f11ea8afe09242451f5c1575f3be3 to your computer and use it in GitHub Desktop.
Player Controller with Ball Jump - Input System
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.InputSystem;
using TMPro;
public class PlayerController2 : MonoBehaviour
{
public float speed;
public int numCubes;
public TextMeshProUGUI countText;
public TextMeshProUGUI winText;
public TextMeshProUGUI oopsText;
private Rigidbody rb;
private int count;
private ArrayList collected = new ArrayList();
private bool jumped;
private float movementX;
private float movementY;
bool action;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
oopsText.text = "";
action = false;
}
void OnMove(InputValue value)
{
Vector2 v = value.Get<Vector2>();
movementX = v.x;
movementY = v.y;
}
void OnJump(InputValue value)
{
action = true;
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
rb.AddForce(movement * speed);
if (this.transform.position.y <= -20)
{
this.transform.position = new Vector3(0, 5, 0);
count = 0;
foreach (Collider thing in collected)
{
thing.gameObject.SetActive(true);
}
collected = new ArrayList();
SetCountText();
oopsText.text = "Oops! Try Again!";
winText.text = "";
}
if (action && !jumped)
{
Vector3 bounce = new Vector3(0.0f, speed * 20, 0.0f);
rb.AddForce(bounce);
jumped = true;
action = false;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
count++;
SetCountText();
collected.Add(other);
}
oopsText.text = "";
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= numCubes)
{
winText.text = "You have won!";
}
}
void OnCollisionStay()
{
jumped = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment