SHOW:
|
|
- or go back to the newest paste.
| 1 | using UnityEngine; | |
| 2 | using System.Collections; | |
| 3 | ||
| 4 | public class Character : MonoBehaviour {
| |
| 5 | ||
| 6 | public float turnSpeed; | |
| 7 | ||
| 8 | Animator animator; | |
| 9 | Vector3 lookPos; | |
| 10 | - | bool onGround; |
| 10 | + | bool onGround = true; |
| 11 | ||
| 12 | // Use this for initialization | |
| 13 | void Start () {
| |
| 14 | ||
| 15 | animator = GetComponentInChildren<Animator>(); | |
| 16 | ||
| 17 | } | |
| 18 | ||
| 19 | // Update is called once per frame | |
| 20 | - | public void Move (float forward, float turn, bool walk, Vector3 lookPos) {
|
| 20 | + | public void Move (Vector3 move, bool walk, Vector3 lookPos) {
|
| 21 | ||
| 22 | Vector3 localMove = transform.InverseTransformDirection(move); | |
| 23 | float turn = Mathf.Atan2( localMove.x, localMove.z ); | |
| 24 | float forward = localMove.magnitude; | |
| 25 | ||
| 26 | this.lookPos = lookPos; | |
| 27 | ||
| 28 | transform.Rotate(0, turn*turnSpeed*Time.deltaTime, 0); | |
| 29 | ||
| 30 | forward *= (walk ? 0.5f : 1); | |
| 31 | Debug.DrawRay(transform.position,rigidbody.velocity*10,Color.yellow); | |
| 32 | ||
| 33 | rigidbody.angularVelocity = Vector3.zero; | |
| 34 | rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); | |
| 35 | - | onGround = false; |
| 35 | + | |
| 36 | ||
| 37 | animator.SetFloat("Forward", forward, 0.1f, Time.deltaTime );
| |
| 38 | animator.SetFloat("Turn", turn, 0.1f, Time.deltaTime );
| |
| 39 | ||
| 40 | animator.applyRootMotion = onGround; | |
| 41 | ||
| 42 | ||
| 43 | ||
| 44 | RaycastHit hit; | |
| 45 | if (Physics.Raycast(new Ray(transform.position, -Vector3.up),out hit, 100)) | |
| 46 | {
| |
| 47 | if (hit.distance > .3f) | |
| 48 | {
| |
| 49 | onGround = false; | |
| 50 | } | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | ||
| 55 | void OnCollisionStay( Collision c ) | |
| 56 | {
| |
| 57 | foreach ( ContactPoint p in c.contacts ) | |
| 58 | {
| |
| 59 | Vector3 localP = transform.InverseTransformPoint( p.point ); | |
| 60 | ||
| 61 | if (localP.magnitude < (collider as CapsuleCollider).radius) | |
| 62 | {
| |
| 63 | onGround = true; | |
| 64 | } | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | ||
| 69 | void OnAnimatorIK(int layerIndex) | |
| 70 | {
| |
| 71 | animator.SetLookAtWeight(1, 0.2f, 2.5f); | |
| 72 | animator.SetLookAtPosition( lookPos ); | |
| 73 | } | |
| 74 | ||
| 75 | ||
| 76 | ||
| 77 | } |