Created
March 28, 2017 10:53
-
-
Save kazumalab/4094575a0952f239be6fe7a07b5fcc70 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 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