using UnityEngine;

using System.Collections;


[RequireComponent(typeof(CharacterController))]


public class CharaterControl : MonoBehaviour {

public AnimationClip idleAnimation;

public AnimationClip walkAnimation;

public AnimationClip runAnimation;

public AnimationClip jumpPoseAnimation;

public AnimationClip fallPoseAnimation;

public Animation animationTarget;

public float idleAnimationSpeed = 0.5f;

public float walkAnimationSpeed = 1.5f;

public float runAnimationSpeed = 1.5f;

public float jumpAnimationSpeed = 4.0f;

public float fallAnimationSpeed = 0.1f;

public float speed = 2.0f;

public float runSpeed = 5.0f;

public float jumpSpeed = 8.0f;

public float gravity = 20.0f;

private CharacterController controller;


//transfortation

private float t_verticalSpeed = 0.0f;

private float t_moveSpeed = 0.0f;

private Vector3 v3_moveDirection = Vector3.zero;


//boolean

private bool b_isRun;

private bool b_isBackward;

private bool b_isJumping;


//rotation

private Quaternion q_currentRotation;

private Quaternion q_rot;

private float t_rotationSpeed = 1.0f;


//vector3

private Vector3 v3_forward;

private Vector3 v3_right;

private CollisionFlags c_collisionFlags;


//air time

private float a_inAirTime = 0.0f;

private float a_inAirStartTime = 0.0f;

private float a_minAirTime = 0.15f;


void Awake(){

controller = GetComponent<CharacterController>();

b_isRun = false;

b_isBackward = false;

b_isJumping = false;

t_moveSpeed = speed;

c_collisionFlags = CollisionFlags.CollidedBelow;

animationTarget.wrapMode = WrapMode.Loop;

animationTarget[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;

animationTarget[fallPoseAnimation.name].wrapMode = WrapMode.ClampForever;

animationTarget[idleAnimation.name].wrapMode = WrapMode.Loop;

  animationTarget[runAnimation.name].wrapMode = WrapMode.Loop;

animationTarget[walkAnimation.name].wrapMode = WrapMode.Loop;

}


// Use this for initialization

void Start () {

a_inAirStartTime = Time.time;

}

public bool IsGrounded(){

return (c_collisionFlags & CollisionFlags.CollidedBelow) != 0;

}

public bool IsJumping(){

return b_isJumping;

}

public bool IsAir(){

return (a_inAirTime > a_minAirTime);

}

public bool IsMoveBackward(){

return b_isBackward;

}

// Update is called once per frame

void Update () {

Transform cameraTransform = Camera.main.transform;


v3_forward = cameraTransform.TransformDirection(Vector3.forward);

v3_forward.y = 0;

v3_right = new Vector3(v3_forward.z, 0, -v3_forward.x);

float t_hor = Input.GetAxis("Horizontal");

float t_ver = Input.GetAxis("Vertical");

if(t_ver < 0){

b_isBackward = true;

}else{

b_isBackward = false;

}


Vector3 v3_targetDirection = (t_hor * v3_right) + (t_ver * v3_forward);


if(v3_targetDirection != Vector3.zero){

v3_moveDirection = Vector3.Slerp(v3_moveDirection, v3_targetDirection ,t_rotationSpeed * Time.deltaTime);

v3_moveDirection = v3_moveDirection.normalized;

}

else{

v3_moveDirection = Vector3.zero;

}

if(!b_isJumping){

if(Input.GetKey(KeyCode.LeftShift)){

b_isRun = true;

t_moveSpeed = runSpeed;

}else{

b_isRun = false;

t_moveSpeed = speed;

}

if(Input.GetButton("Jump")){

t_verticalSpeed = jumpSpeed;

b_isJumping = true;

}

}

if(IsGrounded()){

t_verticalSpeed = 0.0f;

b_isJumping = false;

a_inAirTime = 0.0f;

a_inAirStartTime = Time.time;

}else{

t_verticalSpeed -= gravity * Time.deltaTime;

a_inAirTime = Time.time - a_inAirStartTime;

}

Vector3 v3_movement = (v3_moveDirection * t_moveSpeed) + new Vector3(0, t_verticalSpeed, 0);

v3_movement *= Time.deltaTime;


c_collisionFlags = controller.Move(v3_movement);

//animation play

if(b_isJumping){

if(controller.velocity.y > 0){

animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;

animation.CrossFade(jumpPoseAnimation.name, 0.1f);

}else{

animation[fallPoseAnimation.name].speed = fallAnimationSpeed;

animation.CrossFade(fallPoseAnimation.name, 0.1f);

}

}else{

if(IsAir()){

animation[fallPoseAnimation.name].speed = fallAnimationSpeed;

animation.CrossFade(fallPoseAnimation.name, 0.1f);

}else{

if(controller.velocity.sqrMagnitude < 0.1f){

animation[idleAnimation.name].speed = idleAnimationSpeed;

animation.CrossFade(idleAnimation.name, 0.1f);

}else{

if(b_isRun){

animation[runAnimation.name].speed = runAnimationSpeed;

animation.CrossFade(runAnimation.name, 0.1f);

}else{

animation[walkAnimation.name].speed = walkAnimationSpeed;

animation.CrossFade(walkAnimation.name, 0.1f);

}

}

}

}

//character rotation update/

if(v3_moveDirection != Vector3.zero){

transform.rotation = Quaternion.LookRotation(v3_moveDirection);

}

}

}


'Computer Language > 유니티' 카테고리의 다른 글

[System.Serializable] [Range(0, 100)] [HideInInspector] [SerializeField] Script Editor  (0) 2014.06.23
[ContextMenu("Arrange")] 활용법  (0) 2014.06.23
바둑 주석  (0) 2014.05.29
값과 주소.  (0) 2014.05.26
LookAt()  (0) 2014.05.23

+ Recent posts