Created
July 8, 2020 00:50
-
-
Save niiicolai/ee0e4db339d24fd50a4f0f4a48f103ae to your computer and use it in GitHub Desktop.
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; | |
public class Movement : MonoBehaviour | |
{ | |
[SerializeField] | |
private float movementSpeed; | |
[SerializeField] | |
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; | |
rigidbody.AddForce(velocity); | |
var center = transform.TransformPoint(boxCollider.center); | |
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