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!";
}
}
}
@BacongamingExe
Copy link

It no work lol

@brismithSFHS
Copy link
Author

brismithSFHS commented Jul 17, 2019 via email

@BacongamingExe
Copy link

@brismithSFHS Never mind it works now I was using the wrong version of Visual Studios lol
Thx
:D

@Galaxy-EM6
Copy link

Galaxy-EM6 commented Nov 26, 2020

I am trying to make a ball move:
I put in this exact code (below) but it's not working, I'm using Unity Version 2020.2.0b12,3574:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerControllerBase
{
}
public class PlayerController : MonoBehaviour
{
public float speed = 0;

private Rigidbody rb;

private float movementX;
private float movementY;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
}

private void OnMove(InputValue movementValue)
{
    Vector2 movementVector = movementValue.Get<Vector2>();

    movementX = movementVector.x;
    movementY = movementVector.y;
}

private void FixedUpdate()
{
    Vector3 movement = new Vector3(movementX, 0.0f, movementY);

    rb.AddForce(movement * speed);
}

}

PLS HELP 😞

@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