본문 바로가기
IT/유니티

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

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

 

BulletSprite

using UnityEngine;
using System.Collections;

public class BulletSprite : SpriteBase {
	
	public float Speed = 10.0f;
	
	// Use this for initialization
	void Start () {
		gameObject.AddComponent<Rigidbody>();
		gameObject.rigidbody.useGravity = false;
		gameObject.AddComponent<BoxCollider>();
		gameObject.collider.isTrigger = true;
	}
	
	// Update is called once per frame
	void Update () {
		float moveAmount = Speed * Time.deltaTime;
		transform.Translate(Vector3.up * moveAmount);
		if(transform.position.y > 10.0f)
			Destroy(gameObject);
	}
}

 

CameraRotate

using UnityEngine;
using System.Collections;

public class CameraRotate : MonoBehaviour {
	
	public float Speed = 0.5f;
	
	// Update is called once per frame
	void Update () {
		transform.RotateAround(Vector3.up,Time.deltaTime * Speed);
	}
}

 

EnemySprite

using UnityEngine;
using System.Collections;

public class EnemySprite : SpriteBase {
	
	public float Speed = 10.0f;
	public GameObject explosion;
	void Start () {
		gameObject.AddComponent<BoxCollider>();
	}
	void Update () {
		float moveAmount = Speed * Time.deltaTime;
		transform.Translate(Vector3.down * moveAmount);
		if(transform.position.y < -3.0f)
		{
			ResetPosition();
		}
	}
	void ResetPosition()
	{
		transform.position = new Vector3(Random.Range(-3.0f,3.0f),
			3.0f,0.0f);
	}
	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "bullet")
		{
			Instantiate(explosion,transform.position,transform.rotation);
			ResetPosition();
		}
	}
}

 

MainHandler

using UnityEngine;
using System.Collections;

public class MainHandler : MonoBehaviour {
	
	public int Lives = 3;
	public int Score = 0;
	private float MovingSpeed = 50.0f;
	
	public GameObject DistanceLabel;
	public GameObject LifeLabel;
	public GameObject ScoreLabel;

	// Update is called once per frame
	void Update () {
		DistanceLabel.SendMessage("SetText",
			(Time.time*MovingSpeed).ToString("N1")+" m");
		LifeLabel.SendMessage("SetText","X "+Lives.ToString());
		ScoreLabel.SendMessage("SetText","Score:"+Score.ToString());
	}
	void Bomb(){
		Debug.Log("Bomb Button Click");
	}
}

 

PlayerSprite

using UnityEngine;
using System.Collections;

public class PlayerSprite : SpriteBase {
	
	public GameObject Bullet;
	public float Speed = 5.0f;
	
	float LastShootTime;
	public float ShootDelayTime = 0.2f;
	void Start () {
		LastShootTime = Time.time;
	}
	void Update () {
		float moveAmount = Input.GetAxis("Horizontal")*Speed*Time.deltaTime;
		transform.Translate(Vector3.right * moveAmount);
		if(Time.time > LastShootTime + ShootDelayTime)
		{
			LastShootTime = Time.time;
			Instantiate(Bullet,transform.position,transform.rotation);
		}
	}
}

 

SpriteBase

using UnityEngine;
using System.Collections;

public class SpriteBase : MonoBehaviour {
	
	public Material mat;
	
	void Awake () {
		gameObject.AddComponent<MeshFilter>();
		gameObject.AddComponent<MeshRenderer>();
		Mesh mesh = GetComponent<MeshFilter>().mesh;
		mesh.Clear();
		mesh.vertices = new Vector3[]{new Vector3(0.0f,0.0f,0.0f),new Vector3(0.0f,1.0f,0.0f),
			new Vector3(1.0f,1.0f,0.0f),new Vector3(1.0f,0.0f,0.0f)};
		mesh.uv = new Vector2[]{new Vector2(0.0f,0.0f),new Vector2(0.0f,1.0f),
			new Vector2(1.0f,1.0f),new Vector2(1.0f,0.0f)};
		mesh.triangles = new int[] {0,1,2,0,2,3};
		mesh.RecalculateNormals();
		mesh.RecalculateBounds();
		if(mat != null)
			gameObject.renderer.material = mat;
		MakeResizeByImageSize();
		
	}
	void MakeResizeByImageSize()
	{
		float one_unit = 2.0f/ Screen.height;
		float x = one_unit * mat.mainTexture.width;
		float y = one_unit * mat.mainTexture.height;
		Vector3 newScale = new Vector3(x,y,one_unit);
		transform.localScale = newScale;
	}
}
728x90
그리드형

댓글