duck

Unity 3D Generic Mecanim Character Script

Feb 27th, 2013
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  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;
  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) {
  21.        
  22.         this.lookPos = lookPos;
  23.        
  24.         transform.Rotate(0, turn*turnSpeed*Time.deltaTime, 0);
  25.        
  26.         forward *= (walk ? 0.5f : 1);
  27.        
  28.         rigidbody.angularVelocity = Vector3.zero;
  29.        
  30.         animator.SetFloat("Forward", forward, 0.1f, Time.deltaTime );
  31.         animator.SetFloat("Turn", turn, 0.1f, Time.deltaTime );
  32.        
  33.         animator.applyRootMotion = onGround;
  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.     void OnAnimatorIK(int layerIndex)
  54.     {
  55.         animator.SetLookAtWeight(1, 0.2f, 2.5f);
  56.         animator.SetLookAtPosition( lookPos );
  57.     }
  58.    
  59.    
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment