using UnityEngine;
using System.Collections;
public class playerControls0501 : MonoBehaviour {
public float speed = 3.0F;
public float rotateSpeed = 3.0F;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed, 0);
Vector3 forward = transform.TransformDirection(Vector3.forward);
float curSpeed = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);
}
}
GetComponent : Script가 아닌 ( 게임 오브젝트가 소유하고 있는) 다른 Component들의 레퍼런스도 얻어옴
transform : Script 내에서 직접 접근이 가능한 Component
Input.GetAxis("Horizontal" / "Vertical") : InputManager에 Setting된 좌/우, 상/하 키 입력
transform.TransformDirection(XXX) : 로컬 좌표계의 XXX 방향을 월드 좌표계의 Vector3의 값으로 리턴함
참고 : GetComponent<CharacterController>의 경우 start에서 한번 호출해서 사용하는 것이 바람직한 코딩임
using UnityEngine;
using System.Collections;
public class playerControls0502 : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
CharacterController :
Move : collision에 제한을 받으나 gravity의 영향은 받지 않음(따라서, 중력 값을 세팅할 필요가 있음)
Input.GetButton : InputManager에 Setting된 특정 키에 대한 입력
Time.deltaTime : 매프레임의 경과 시간
public 변수 : Unity Inspect view에서 확인할 수 있음
실행타임에 값을 바꿀 수 있으며, Inspector창에서 세팅된 값이 Script의 클래스 초기 값 세팅 값보다 우선시 됨
테스트 실습
GetButton/ GetButtonDown 출력을 통해 실습해 보기
Time.time / Time.deltaTime 출력해보기
using UnityEngine;
using System.Collections;
public class playerControls0601 : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private aniSprite aniSpr;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
if (!controller.isGrounded)
{
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.x *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls0602 : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
private aniSprite aniSpr;
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
if(Input.GetButtonDown("Jump") && Input.GetButton("Fire1"))
{
moveDirection.y = jumpSpeed + 5;
}
}
if (!controller.isGrounded)
{
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.x *= speed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls0701 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
private Vector3 velocity = Vector3.zero;
private aniSprite aniSpr;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (Input.GetButtonDown("Jump") && !Input.GetButton("Fire1"))
{
velocity.y = walkJump;
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1"))
{
velocity.y = runJump;
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal"); //Input.GetAxis("Vertical"));
velocity.x *= walkSpeed;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls0801 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
private Vector3 velocity = Vector3.zero;
private aniSprite aniSpr;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
}
void Update()
{
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (Input.GetButtonDown("Jump") && !Input.GetButton("Fire1"))
{
velocity.y = walkJump;
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1"))
{
velocity.y = runJump;
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal"); //Input.GetAxis("Vertical"));
velocity.x *= walkSpeed;
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls0901 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
private Vector3 velocity = Vector3.zero;
private aniSprite aniSpr;
private CharacterController controller;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
if (moveDirection == 0)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 9, 16, 24);
}
if (moveDirection == 1)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 8, 16, 24);
}
}
if (Input.GetButtonDown("Jump") && !Input.GetButton("Fire1"))
{
velocity.y = walkJump;
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1"))
{
velocity.y = runJump;
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal"); //Input.GetAxis("Vertical"));
velocity.x *= walkSpeed;
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls1001 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
private Vector3 velocity = Vector3.zero;
private bool jumpEnable = false;
private bool runJumpEnable = false;
private bool crouchJumpEnable = false;
private aniSprite aniSpr;
private CharacterController controller;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
if (moveDirection == 0)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 9, 16, 24);
}
if (moveDirection == 1)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 8, 16, 24);
}
}
if (Input.GetButtonDown("Jump") && (!Input.GetButton("Fire1")|| Input.GetButton("Fire1") && velocity.x == 0) && Input.GetAxis("Vertical") >=0 )
{
velocity.y = walkJump;
jumpEnable = true;
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1") && velocity.x != 0 )
{
velocity.y = runJump;
runJumpEnable = true;
}
if (Input.GetButtonDown("Jump") && velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
velocity.y = crouchJump;
crouchJumpEnable = true;
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal");
if (Input.GetButtonUp("Jump")) // Á¡ÇÁ ŰžŠ ³õŽÂ Œø°£ Á¡ÇÁ ŸÈÇϰÔ
{
velocity.y = velocity.y - fallSpeed;
//print(" Jump Draw");
}
if (moveDirection == 0)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 11, 4, 12);
}
}
if (moveDirection == 1)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 10, 4, 12);
}
}
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
using UnityEngine;
using System.Collections;
public class playerControls1101 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
private Vector3 velocity = Vector3.zero;
private bool jumpEnable = false;
private bool runJumpEnable = false;
private bool crouchJumpEnable = false;
private float afterHitForceDown = 1.0F;
private aniSprite aniSpr;
private CharacterController controller;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
controller = GetComponent<CharacterController>();
}
void Update()
{
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
if (moveDirection == 0)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 9, 16, 24);
}
if (moveDirection == 1)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 8, 16, 24);
}
}
if (Input.GetButtonDown("Jump") && (!Input.GetButton("Fire1")|| Input.GetButton("Fire1") && velocity.x == 0) && Input.GetAxis("Vertical") >=0 )
{
velocity.y = walkJump;
jumpEnable = true;
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1") && velocity.x != 0)
{
velocity.y = runJump;
runJumpEnable = true;
}
if (Input.GetButtonDown("Jump") && velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
velocity.y = crouchJump;
crouchJumpEnable = true;
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal");
if (Input.GetButtonUp("Jump")) // Á¡ÇÁ ŰžŠ ³õŽÂ Œø°£ Á¡ÇÁ ŸÈÇϰÔ
{
velocity.y = velocity.y - fallSpeed;
//print(" Jump Draw");
}
if (moveDirection == 0)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 11, 4, 12);
}
}
if (moveDirection == 1)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 10, 4, 12);
}
}
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
if (controller.collisionFlags == CollisionFlags.Above)
{
velocity.y = 0;
velocity.y -= afterHitForceDown;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
controller.collsionFlags :
using UnityEngine;
using System.Collections;
public class playerControls1201 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
public AudioClip soundJump;
public AudioClip soundCrouchJump;
private float soundRate = 0.0f;
private float soundDelay = 0.0f;
private Vector3 velocity = Vector3.zero;
private bool jumpEnable = false;
private bool runJumpEnable = false;
private bool crouchJumpEnable = false;
private float afterHitForceDown = 1.0F;
private aniSprite aniSpr;
private CharacterController controller;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
controller = GetComponent<CharacterController>();
}
IEnumerator PlaySound(AudioClip soundName, float soundDelay)
{
if (!audio.isPlaying && Time.time > soundRate)
{
soundRate = Time.time + soundDelay;
audio.clip = soundName;
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
}
}
void Update()
{
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
if (moveDirection == 0)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 9, 16, 24);
}
if (moveDirection == 1)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 8, 16, 24);
}
}
if (Input.GetButtonDown("Jump") && !Input.GetButton("Fire1"))
{
velocity.y = walkJump;
jumpEnable = true;
StartCoroutine(PlaySound(soundJump, 0));
}
if (Input.GetButtonDown("Jump") && Input.GetButton("Fire1"))
{
velocity.y = runJump;
runJumpEnable = true;
StartCoroutine(PlaySound(soundJump, 0));
}
if (Input.GetButtonDown("Jump") && velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
velocity.y = crouchJump;
crouchJumpEnable = true;
StartCoroutine(PlaySound(soundCrouchJump, 0));
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal");
if (Input.GetButtonUp("Jump")) // Á¡ÇÁ ŰžŠ ³õŽÂ Œø°£ Á¡ÇÁ ŸÈÇϰÔ
{
velocity.y = velocity.y - fallSpeed;
//print(" Jump Draw");
}
if (moveDirection == 0)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 11, 4, 12);
}
}
if (moveDirection == 1)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 10, 4, 12);
}
}
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
if (controller.collisionFlags == CollisionFlags.Above)
{
velocity.y = 0;
velocity.y -= afterHitForceDown;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
audio 변수 : Unity 자체에서 연결해 주는 변수
Script가 부착된 GameObject의 Component인 Audio Source를 가리킴
using UnityEngine;
using System.Collections;
public class playerControls1202 : MonoBehaviour {
public float walkSpeed = 1.5F;
public float runSpeed = 2.0F;
public float fallSpeed = 2.0F;
public float walkJump = 6.2F;
public float runJump = 9.0F;
public float crouchJump = 10.0F;
public float gravity = 20.0F;
public float startPos = 0.0F;
public int moveDirection = 1;
public Transform particleJump;
public AudioClip soundJump;
public AudioClip soundCrouchJump;
private float soundRate = 0.0f;
private float soundDelay = 0.0f;
private Vector3 velocity = Vector3.zero;
private bool jumpEnable = false;
private bool runJumpEnable = false;
private bool crouchJumpEnable = false;
private float afterHitForceDown = 1.0F;
private aniSprite aniSpr;
private CharacterController controller;
// Use this for initialization
void Start()
{
aniSpr = GetComponent<aniSprite>();
controller = GetComponent<CharacterController>();
}
IEnumerator PlaySound(AudioClip soundName, float soundDelay)
{
if (!audio.isPlaying && Time.time > soundRate)
{
soundRate = Time.time + soundDelay;
audio.clip = soundName;
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
}
}
void Update()
{
Vector3 particlePlacement = new Vector3(transform.position.x, transform.position.y - 0.5F, transform.position.z);
if (controller.isGrounded)
{
velocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0); //Input.GetAxis("Vertical"));
if (velocity.x == 0 && moveDirection == 1)
{
aniSpr.aniPlay(16, 16, 0, 0, 16, 12);
}
if (velocity.x == 0 && moveDirection == 0)
{
aniSpr.aniPlay(16, 16, 0, 1, 16, 12);
}
if (velocity.x < 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 3, 10, 15);
}
if (velocity.x > 0)
{
velocity *= walkSpeed;
aniSpr.aniPlay(16, 16, 0, 2, 10, 15);
}
if (velocity.x < 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 5, 16, 24);
}
if (velocity.x > 0 && Input.GetButton("Fire1"))
{
velocity *= runSpeed;
aniSpr.aniPlay(16, 16, 0, 4, 16, 24);
}
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
if (moveDirection == 0)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 9, 16, 24);
}
if (moveDirection == 1)
{
velocity.x = 0;
aniSpr.aniPlay(16, 16, 0, 8, 16, 24);
}
}
if (Input.GetButtonDown("Jump"))
{
if (Input.GetButton("Fire1"))
{
velocity.y = runJump;
Instantiate(particleJump, particlePlacement, transform.rotation);
runJumpEnable = true;
StartCoroutine(PlaySound(soundJump, 0));
}
else
{
if (velocity.x == 0 && Input.GetAxis("Vertical") < 0)
{
velocity.y = crouchJump;
Instantiate(particleJump, particlePlacement, transform.rotation);
crouchJumpEnable = true;
StartCoroutine(PlaySound(soundCrouchJump, 0));
}
else
{
velocity.y = walkJump;
Instantiate(particleJump, particlePlacement, transform.rotation);
jumpEnable = true;
StartCoroutine(PlaySound(soundJump, 0));
}
}
}
}
if (!controller.isGrounded)
{
velocity.x = Input.GetAxis("Horizontal");
if (Input.GetButtonUp("Jump")) // Á¡ÇÁ ŰžŠ ³õŽÂ Œø°£ Á¡ÇÁ ŸÈÇϰÔ
{
velocity.y = velocity.y - fallSpeed;
//print(" Jump Draw");
}
if (moveDirection == 0)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 3, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 11, 4, 12);
}
}
if (moveDirection == 1)
{
if (jumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (runJumpEnable)
{
velocity.x *= runSpeed;
aniSpr.aniPlay(16, 16, 11, 2, 4, 12);
}
if (crouchJumpEnable)
{
velocity.x *= walkSpeed;
aniSpr.aniPlay(16, 16, 12, 10, 4, 12);
}
}
}
if (velocity.x < 0)
{
moveDirection = 0;
}
if (velocity.x > 0)
{
moveDirection = 1;
}
if (controller.collisionFlags == CollisionFlags.Above)
{
velocity.y = 0;
velocity.y -= afterHitForceDown;
}
velocity.y -= gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}