using UnityEngine;
using System.Collections;
public class PlayerCtrl : MonoBehaviour {
[System.Serializable]
public class Anims
{
public AnimationClip idle;
public AnimationClip runForward;
public AnimationClip runBackward;
public AnimationClip runLeft;
public AnimationClip runRight;
}
public Anims anims;
public Animation anybody;
public float Speed = 10.0f;
public GameObject bulletPrefab;
public Transform FirePos;
// Use this for initialization
void Start () {
anybody.clip = anims.runForward;
anybody.Play();
}
// Update is called once per frame
void Update () {
float v = Input.GetAxis("Vertical");
float h = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * Speed * v * Time.deltaTime);
transform.Translate(Vector3.right * Speed * h * Time.deltaTime);
if(v>0.1f)
{
anybody.CrossFade(anims.runForward.name,0.3f);
}
else if(v<-0.1f)
{
anybody.CrossFade(anims.runBackward.name,0.3f);
}
else if(h>0.1f)
{
anybody.CrossFade(anims.runRight.name,0.3f);
}
else if(h<-0.1f)
{
anybody.CrossFade(anims.runLeft.name,0.3f);
}
else
anybody.CrossFade(anims.idle.name, 0.3f);
if(Input.GetMouseButtonDown(0))
{
StartCoroutine("Fire"); // 함수이름을 호출하는 부분. 멀티쓰레드로 업데이트 함수의 부담을 나눈다.
}
}
IEnumerator Fire()
{ // 무엇을 어디서 얼마나 회전시켜서 발사할 것인가를 GameObject타입의 newBullet에 대입한다.
GameObject newBullet = Instantiate(bulletPrefab,FirePos.position,
Quaternion.identity)as GameObject; // 동적으로 할당해 주는것
Destroy(newBullet,3.0f);
yield return null;
/*
GameObject newBullet = (GameObject)Instantiate(bulletPrefab,FirePos.position,
Quaternion.identity); 묵시적 형 변환
*/
}
}
'유니티3D > 예제' 카테고리의 다른 글
Spark.cs (0) | 2013.08.06 |
---|---|
bulletCtrl (0) | 2013.07.25 |
PlayerCrtl (0) | 2013.07.25 |
PlayerCtrl_소스 (0) | 2013.07.24 |
자동차게임 전체 스크립트 (0) | 2013.07.23 |