Last active
November 3, 2021 01:16
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Galaxy-EM6
![Screenshot (22)](https://user-images.githubusercontent.com/75196182/102178049-92276980-3e59-11eb-9622-ae9d66297ab1.png)
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.
I used your code and that was the only thing I needed to do.