duck

Mecanim Character - Step 2

Feb 27th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Character : MonoBehaviour {
  5.  
  6.     public float moveSpeed;
  7.    
  8.     Animator animator;
  9.     bool onGround;
  10.    
  11.     // Use this for initialization
  12.     void Start () {
  13.    
  14.         animator = GetComponentInChildren<Animator>();
  15.        
  16.     }
  17.    
  18.     // Update is called once per frame
  19.     void FixedUpdate () {
  20.    
  21.         bool walk = Input.GetKey(KeyCode.LeftShift);
  22.        
  23.         float v = Input.GetAxis("Vertical");
  24.         float h = Input.GetAxis("Horizontal");
  25.        
  26.         v *= (walk ? 0.5f : 1);
  27.        
  28.         rigidbody.angularVelocity = Vector3.zero;
  29.        
  30.         animator.SetFloat("Forward", v, 0.1f, Time.deltaTime );
  31.        
  32.         animator.applyRootMotion = onGround;
  33.        
  34.        
  35.         onGround = false;
  36.     }
  37.    
  38.    
  39.     void OnCollisionStay( Collision c )
  40.     {
  41.         foreach ( ContactPoint p in c.contacts )
  42.         {
  43.             Vector3 localP = transform.InverseTransformPoint( p.point );
  44.            
  45.             if (localP.magnitude < (collider as CapsuleCollider).radius)
  46.             {
  47.                 onGround = true;
  48.             }
  49.         }
  50.     }
  51.    
  52.    
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment