Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2020 00:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save niiicolai/ee0e4db339d24fd50a4f0f4a48f103ae to your computer and use it in GitHub Desktop.
using UnityEngine;
public class Movement : MonoBehaviour
private float movementSpeed;
private float jumpPower;
private bool grounded;
private float groundCheckDistance = 1;
private float boxColliderHeight = .1f;
private float sizeDivisor = 1.1f;
private Vector3 size;
private BoxCollider boxCollider;
private new Rigidbody rigidbody;
void Start()
boxCollider = GetComponent<BoxCollider>();
rigidbody = GetComponent<Rigidbody>();
size = new Vector3(boxCollider.size.x / sizeDivisor, boxColliderHeight, boxCollider.size.z / sizeDivisor);
void Update()
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 velocity = new Vector3(h, 0, v);
velocity = transform.TransformDirection(velocity);
velocity *= movementSpeed;
velocity -= rigidbody.velocity;
var center = transform.TransformPoint(;
grounded = Physics.BoxCast(center, size, Vector3.down, transform.rotation, groundCheckDistance);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddForce(Vector3.up * jumpPower);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment