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 moveSpeed; |
| 6 | + | |
| 7 | public float turnSpeed; | |
| 8 | ||
| 9 | Animator animator; | |
| 10 | Transform look; | |
| 11 | ||
| 12 | bool onGround; | |
| 13 | ||
| 14 | // Use this for initialization | |
| 15 | void Start () {
| |
| 16 | ||
| 17 | look = GetComponentInChildren<MouseLookPivot>().transform; | |
| 18 | animator = GetComponentInChildren<Animator>(); | |
| 19 | ||
| 20 | } | |
| 21 | ||
| 22 | // Update is called once per frame | |
| 23 | void FixedUpdate () {
| |
| 24 | ||
| 25 | bool walk = Input.GetKey(KeyCode.LeftShift); | |
| 26 | ||
| 27 | float v = Input.GetAxis("Vertical");
| |
| 28 | float h = Input.GetAxis("Horizontal");
| |
| 29 | ||
| 30 | - | animator.SetFloat("Forward", v, 0.1f, Time.deltaTime );
|
| 30 | + | Vector3 move = v * look.forward + h * look.right; |
| 31 | Vector3 localMove = transform.InverseTransformDirection(move); | |
| 32 | ||
| 33 | float turn = Mathf.Atan2( localMove.x, localMove.z ); | |
| 34 | ||
| 35 | transform.Rotate(0, turn*turnSpeed*Time.deltaTime, 0); | |
| 36 | ||
| 37 | v *= (walk ? 0.5f : 1); | |
| 38 | ||
| 39 | rigidbody.angularVelocity = Vector3.zero; | |
| 40 | ||
| 41 | animator.SetFloat("Forward", localMove.magnitude, 0.1f, Time.deltaTime );
| |
| 42 | animator.SetFloat("Turn", turn, 0.1f, Time.deltaTime );
| |
| 43 | ||
| 44 | animator.applyRootMotion = onGround; | |
| 45 | ||
| 46 | ||
| 47 | onGround = false; | |
| 48 | } | |
| 49 | ||
| 50 | ||
| 51 | void OnCollisionStay( Collision c ) | |
| 52 | {
| |
| 53 | foreach ( ContactPoint p in c.contacts ) | |
| 54 | {
| |
| 55 | Vector3 localP = transform.InverseTransformPoint( p.point ); | |
| 56 | ||
| 57 | if (localP.magnitude < (collider as CapsuleCollider).radius) | |
| 58 | {
| |
| 59 | onGround = true; | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | ||
| 64 | ||
| 65 | ||
| 66 | } |