Skip to content

Instantly share code, notes, and snippets.

@jawinn
Created July 26, 2015 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jawinn/a8b0ee42537d9379af2a to your computer and use it in GitHub Desktop.
Save jawinn/a8b0ee42537d9379af2a to your computer and use it in GitHub Desktop.
Unity Bare Bones Top-Down Controller C#
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 6.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public CharacterController controller;
void Start(){
// Store reference to attached component
controller = GetComponent<CharacterController>();
}
void Update()
{
// Character is on ground (built-in functionality of Character Controller)
if (controller.isGrounded)
{
// Use input up and down for direction, multiplied by speed
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Apply gravity manually.
moveDirection.y -= gravity * Time.deltaTime;
// Move Character Controller
controller.Move(moveDirection * Time.deltaTime);
}
}
@ishanchandane101
Copy link

Thanks! Really saved me a lot of time!

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