본문 바로가기
IT/유니티

유니티 게임 프로그래밍 5장 예제 구현

by nutrient 2021. 5. 30.
728x90
728x170

 

 

BulletControl

using UnityEngine;
using System.Collections;

public class BulletControl : MonoBehaviour {
	
	public float Speed = 20.0f;
	
	
	// Update is called once per frame
	void Update () {
		float moveAmt = Time.deltaTime * Speed;
		transform.Translate(Vector3.forward * moveAmt);
		if(transform.position.z > 35.0f)
		{
			Destroy(gameObject);
		}
	}
}

 

EnemyControl

using UnityEngine;
using System.Collections;

public class EnemyControl : MonoBehaviour {
	
	public float Speed = 8.0f;
	public GameObject explosion;
	
	
	// Update is called once per frame
	void Update () {
		float moveAmt = Speed * Time.deltaTime;
		
		transform.Translate(Vector3.back * moveAmt);
		
		if(transform.position.z < -7.0f)
		{
			InitPosition();
		}
	}
	
	void InitPosition()
	{
		transform.position = new Vector3(Random.Range(-10.0f,10.0f),
			Random.Range(-10.0f,10.0f),35.0f);
		
	}
	
	void OnTriggerEnter(Collider other)
	{
		if(other.gameObject.tag == "bullet")
		{
			Main.Score += 100;
			audio.Play();
			Instantiate(explosion,transform.position,transform.rotation);
			InitPosition();
			
		}
	}
}

 

Main

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {
	
	static public int Score = 0;
	static public int Lives = 3;
	
	public int FinishScore = 1000;

	
	// Update is called once per frame
	void Update () {
		if(Score >= FinishScore)
		{
			Score = 0;
			Application.LoadLevel("win");
		}
	}
	
	void OnGUI(){
		GUI.Label(new Rect(10.0f,10.0f,200.0f,20.0f),Main.Score.ToString());
		GUI.Label(new Rect(10.0f,30.0f,200.0f,20.0f),Main.Lives.ToString());
	}
}

 

PlayerControl

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {
	
	public float Speed = 1.0f;
	public GameObject bullet;
	
	// Update is called once per frame
	void Update () {
		
		float moveAmt = Input.GetAxis("Horizontal") * Speed * Time.deltaTime;
		float moveAmt2 = Input.GetAxis("Vertical") * Speed * Time.deltaTime;
		Vector3 moveVector = Vector3.right * moveAmt + Vector3.up * moveAmt2;
		transform.Translate(moveVector);
		if(Input.GetKeyDown(KeyCode.Space))
		{
			audio.Play();
			Instantiate(bullet,transform.position,transform.rotation);
		}
	}
}

 

Win

using UnityEngine;
using System.Collections;

public class Win : MonoBehaviour {

	
	
	void OnGUI(){
		if(GUI.Button(new Rect(Screen.width/2-100.0f,Screen.height/2-100.0f,200.0f,200.0f),"You Win~!"))
		{
			
			Application.LoadLevel("game");
		}
	}
}

 

 

shooting_mesh.unitypackage
3.74MB

728x90
그리드형

댓글