Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Character : MonoBehaviour {
- public float turnSpeed;
- Animator animator;
- Vector3 lookPos;
- bool onGround = true;
- // Use this for initialization
- void Start () {
- animator = GetComponentInChildren<Animator>();
- }
- // Update is called once per frame
- public void Move (Vector3 move, bool walk, Vector3 lookPos) {
- Vector3 localMove = transform.InverseTransformDirection(move);
- float turn = Mathf.Atan2( localMove.x, localMove.z );
- float forward = localMove.magnitude;
- this.lookPos = lookPos;
- transform.Rotate(0, turn*turnSpeed*Time.deltaTime, 0);
- forward *= (walk ? 0.5f : 1);
- Debug.DrawRay(transform.position,rigidbody.velocity*10,Color.yellow);
- rigidbody.angularVelocity = Vector3.zero;
- rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0);
- animator.SetFloat("Forward", forward, 0.1f, Time.deltaTime );
- animator.SetFloat("Turn", turn, 0.1f, Time.deltaTime );
- animator.applyRootMotion = onGround;
- RaycastHit hit;
- if (Physics.Raycast(new Ray(transform.position, -Vector3.up),out hit, 100))
- {
- if (hit.distance > .3f)
- {
- onGround = false;
- }
- }
- }
- void OnCollisionStay( Collision c )
- {
- foreach ( ContactPoint p in c.contacts )
- {
- Vector3 localP = transform.InverseTransformPoint( p.point );
- if (localP.magnitude < (collider as CapsuleCollider).radius)
- {
- onGround = true;
- }
- }
- }
- void OnAnimatorIK(int layerIndex)
- {
- animator.SetLookAtWeight(1, 0.2f, 2.5f);
- animator.SetLookAtPosition( lookPos );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment