Skip to content

Instantly share code, notes, and snippets.

@kazumalab
Created March 28, 2017 10:53
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 kazumalab/4094575a0952f239be6fe7a07b5fcc70 to your computer and use it in GitHub Desktop.
Save kazumalab/4094575a0952f239be6fe7a07b5fcc70 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControl : MonoBehaviour {
public bool isGround = false;
[HideInInspector]public Rigidbody rb;
private float height = 0f;
private bool grounded = false;
private Vector3 preDirection;
private void Start () {
Init ();
getHeight ();
}
private void Update () {
getGrounded ();
}
private void Init () {
rb = gameObject.GetComponent<Rigidbody> ();
if (rb == null) {
rb = gameObject.AddComponent<Rigidbody> ();
}
}
private void getGrounded () {
RaycastHit hit;
if (Physics.Raycast (transform.position, -Vector3.up, out hit)) {
if (hit.distance - height < 0.1) {
if (grounded) {
isGround = true;
}
} else {
isGround = false;
}
}
}
private void getHeight () {
height = transform.localScale.y;
}
void OnCollisionEnter (Collision col) {
grounded = true;
}
public void Move (Vector3 v, Vector3 d) {
rb.velocity = v;
if (v != Vector3.zero) {
transform.LookAt (transform.position + d);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment