Created
July 26, 2015 17:13
-
-
Save jawinn/a8b0ee42537d9379af2a to your computer and use it in GitHub Desktop.
Unity Bare Bones Top-Down Controller C#
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 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Really saved me a lot of time!