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

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;
 
 // Use this for initialization
 void Start () {
  
  anybody.clip = anims.idle;
  //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 * v * Time.deltaTime * Speed);
  transform.Translate(Vector3.right * h * Time.deltaTime * Speed);
  
  
  
  
  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.runLeft.name,0.3f); 
  }
  else if(h > 0.1f){
   anybody.CrossFade(anims.runRight.name,0.3f); 
  }else
   anybody.CrossFade(anims.idle.name,0.3f);
  
 }
}

'유니티3D > 예제' 카테고리의 다른 글

bulletCtrl  (0) 2013.07.25
PlayerCtrl_V2  (0) 2013.07.25
PlayerCtrl_소스  (0) 2013.07.24
자동차게임 전체 스크립트  (0) 2013.07.23
도로밖으로 차 못나가게 하기  (0) 2013.07.22

 

 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;
 
 // Use this for initialization
 void Start () {
  
  anybody.clip = anims.idle;
  //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 * v * Time.deltaTime * Speed);
  transform.Translate(Vector3.right * h * Time.deltaTime * Speed);
  
  
  
  
  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.runLeft.name,0.3f); 
  }
  else if(h > 0.1f){
   anybody.CrossFade(anims.runRight.name,0.3f); 
  }else
   anybody.CrossFade(anims.idle.name,0.3f);
  
 }
}

 

'유니티3D > 예제' 카테고리의 다른 글

PlayerCtrl_V2  (0) 2013.07.25
PlayerCrtl  (0) 2013.07.25
자동차게임 전체 스크립트  (0) 2013.07.23
도로밖으로 차 못나가게 하기  (0) 2013.07.22
자동차 좌우 이동  (0) 2013.07.22

 

using UnityEngine;
using System.Collections;

public class Road : MonoBehaviour {
 
 public float speed = 3.0f;    // public을 넣지 않으면 Inspector에서 수정불가능.
 public float x = 0;
 public float y = 0;
 public float roadHeight = 6.0f;
 public float roadWidth = 6.0f;
 
 // Use this for initialization
 void Start () {   // 시작하기 전에 세팅이나 초기화.
  
  
 
 }
 
 // Update is called once per frame
 void Update () { // 게임이 종료될때 까지 계속해서 구동되는 부분.
  
  y = y + Time.deltaTime * speed;
  renderer.material.mainTextureOffset = new Vector2(x,-y);  // 위에서 밑으로 내려오는 건 음수 올라가는건 양수.
  // x z 축은 안움직이기 때문에 Vector2로 기본값 설정.
 }
}
 

 using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
 
 public float Speed = 10.0f;
 // Use this for initialization
 void Start () {
  
 }
 
 // Update is called once per frame
 void Update () {
  float h = Input.GetAxis("Horizontal");    //input ->  입력  좌우로 움직이는 명령어Time.deltaTime.
  float v = Input.GetAxis("Vertical");
  
  transform.Translate(Vector3.right * h * Speed *Time.deltaTime);      //왼쪽으로 움직이면 음수값이 나오기 때문에 left는 따로 적을필요 없다.
  transform.Translate(Vector3.up * v * Speed *Time.deltaTime);
  
  /*
  float h = Input.GetAxis("Horizontal")*Time.deltaTime;      한문장으로 사용하는법.
  float v = Input.GetAxis("Vertical")*Time.deltaTime;
      */
  
  // 도로밖으로 차 못나가게 하기
  
  if(transform.position.x > 6.0f )
  {
   transform.position = new Vector3(6.0f,transform.position.y,transform.position.z);
  }
  if(transform.position.x < -6.0f)
  {
   transform.position = new Vector3(-6.0f,transform.position.y,transform.position.z);
  }
  
  if(transform.position.y > 7.0f )
  {
   transform.position = new Vector3(transform.position.x,7.0f,transform.position.z);
  }
  if(transform.position.y < -7.0f)
  {
   transform.position = new Vector3(transform.position.x,-7.0f,transform.position.z);
  }
   
 }
}

 using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
 
 public float Speed = 1.5f;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
  
  transform.Translate (Vector3.down * Speed * Time.deltaTime);
 
 }
}

 using UnityEngine;
using System.Collections;

public class SpawnerCar : MonoBehaviour {
 
 public GameObject Prefab1;
 public float nextTime = 0.0f;
 public float Rate = 0.5f;
 
 public Road road;
 // Use this for initialization
 void Start () {
  
  road = GameObject.Find("Road").GetComponent<Road>();      //하이라키 값. 스크립트값.
 
 }
 
 // Update is called once per frame
 void Update () {
  
  if(Time.time  > nextTime)
  {
   int i = Random.Range(1,4);  //실제로는 0~3 의 값
   if(i ==1)
   {
    EnemyCar1();
   }
   nextTime = Time.time + Rate;
  }
 
 }
 
 void EnemyCar1()
 {
  float addxpos = Random.Range(road.roadWidth, -road.roadWidth);
  Vector3 pos = new Vector3(addxpos,0,0) + transform.position;
  
  Instantiate(Prefab1,pos,Quaternion.identity); // 발사~ Prefab1을...무회전으로 하겠다.
 }
 
 
}

 

'유니티3D > 예제' 카테고리의 다른 글

PlayerCrtl  (0) 2013.07.25
PlayerCtrl_소스  (0) 2013.07.24
도로밖으로 차 못나가게 하기  (0) 2013.07.22
자동차 좌우 이동  (0) 2013.07.22
도로 움직이는 예제  (0) 2013.07.22

+ Recent posts