Skip to content

Instantly share code, notes, and snippets.

@brismithSFHS
Last active November 3, 2021 01:16
Show Gist options
  • Save brismithSFHS/8dd4439b3857066665d3e69c5a3b5fac to your computer and use it in GitHub Desktop.
Save brismithSFHS/8dd4439b3857066665d3e69c5a3b5fac to your computer and use it in GitHub Desktop.
The finished Player Controller script for Unity Roll-a-Ball Tutorial with jumping and score keeping
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public float jumpHeight;
public Text countText;
public Text winText;
public int numPickups;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
if (Input.GetKeyDown("space"))
{
Vector3 jump = new Vector3 (0.0f, jumpHeight, 0.0f);
rb.AddForce (jump);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count++;
SetCountText();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= numPickups)
{
winText.text = "You win!";
}
}
}
@FlatMilkGames
Copy link

FlatMilkGames commented Dec 15, 2020

@Galaxy-EM6
I think you may have found it out by now and I probably don't know much more than you but, In the unity editor have you clicked save asset inside of your Input actions ? If so, after that you need to select your Input actions in the Assets and tell it to generate a c# class, you can leave the fields blank, then press apply. And as far as the code goes I don't see any reason for it to not work except that currently your speed is set to 0 (unless you changed it in the editor) and your player character might not have a rigidbody or the rigidbody is part of the child. Also, most importantly, make sure there is a player Input component with the Input actions on it attached to the game object.
Screenshot (22)
I used your code and that was the only thing I needed to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment