using UnityEngine;
using System.Collections;
// 플레이어의 움직임을 처리하는 클래스
public class Player_1 : MonoBehaviour {
// 플레이어의 속도
public float speed = 10.0f;
// 플레이어 컨트롤러(터치 or 키보드)
public bool IsTouch = false;
// 플레이어의 최대 최소 이동범위를 위한 변수들
Vector2 Screen_Size;
Vector2 Player_SpriteSize;
// Use this for initialization
void Start () {
Screen_Size = new Vector2 (Screen.width / 100.0f, Screen.height / 100.0f);
Player_SpriteSize = this.gameObject.GetComponent<SpriteRenderer> ().sprite.textureRect.center/100.0f;
}
// Update is called once per frame
void Update () {
if (IsTouch == false) {
KeyBoardControl();
}
}
void KeyBoardControl(){
// 캐릭터 좌우 이동, 화면 밖으로 나갔을때 처리
float dirX = Input.GetAxis ("Horizontal");
this.gameObject.transform.Translate (Vector3.right * dirX * speed*Time.deltaTime);
// 캐릭터가 화면 왼쪽으로 나갔을때 X 최소값으로 되돌린다.
if (this.gameObject.transform.position.x < -Screen_Size.x+(Player_SpriteSize.x)) {
this.gameObject.transform.position = new Vector3(-Screen_Size.x+(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터가 화면 오른쪽으로 나갔을때 X 최대값으로 되돌린다.
else if(this.gameObject.transform.position.x > Screen_Size.x-(Player_SpriteSize.x)){
this.gameObject.transform.position = new Vector3(Screen_Size.x-(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터 상하 이동, 화면 밖으로 나갔을때 처리
float dirY = Input.GetAxis ("Vertical");
this.gameObject.transform.Translate (Vector3.up * dirY * speed*Time.deltaTime);
// 캐릭터가 화면 아래로 나갔을때 Y최소값으로 되돌린다.
if (this.gameObject.transform.position.y < -Screen_Size.y+(Player_SpriteSize.y)) {
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, -Screen_Size.y+(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
// 캐릭터가 화면 아래로 나갔을때 Y최대값으로 되돌린다.
else if(this.gameObject.transform.position.y > Screen_Size.y-(Player_SpriteSize.y)){
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, Screen_Size.y-(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
}
}
using UnityEngine;
using System.Collections;
// 총알의 움직임을 직선으로 처리하는 클래스
// X, Y의 방향을 설정해주면 해당 방향으로 이동함
public class BulletMoveLine : MonoBehaviour {
// 이동할 X,Y방향
public float moveX = 0.0f;
public float moveY = 0.0f;
// 총알의 속도
public float speed = 0.0f;
// 총알의 이동 각도
float Angle = 0.0f;
// 화면 안의 거리를 체크
Vector2 LimitDistance;
// Use this for initialization
void Start () {
LimitDistance = new Vector2 (Screen.width / 100.0f, Screen.height / 100.0f);
}
// Update is called once per frame
void Update () {
// 일직선으로 이동
this.gameObject.transform.Translate (new Vector3 (moveX, moveY, 0) * speed * Time.deltaTime);
// 화면 밖일 경우 제거
if (this.gameObject.transform.position.y < -LimitDistance.y || this.gameObject.transform.position.y > LimitDistance.y
|| this.gameObject.transform.position.x < -LimitDistance.x || this.gameObject.transform.position.x > LimitDistance.x){
Destroy(this.gameObject);
}
}
}
using UnityEngine;
using System.Collections;
// 플레이어의 움직임을 처리하는 클래스
public class Player_2 : MonoBehaviour {
// 플레이어의 속도
public float speed = 10.0f;
// 플레이어 컨트롤러(터치 or 키보드)
public bool IsTouch = false;
// 플레이어의 최대 최소 이동범위를 위한 변수들
Vector2 Screen_Size;
Vector2 Player_SpriteSize;
// 플레이어의 총알
public GameObject bullet;
// 총알 발사 딜레이(자동)
public float Bullet_Delay = 0.0f;
float Bullet_DeltaTime = 0.0f;
// Use this for initialization
void Start () {
Screen_Size = new Vector2 (Screen.width / 100.0f, Screen.height / 100.0f);
Player_SpriteSize = this.gameObject.GetComponent<SpriteRenderer> ().sprite.textureRect.center/100.0f;
}
// Update is called once per frame
void Update () {
if (IsTouch == false) {
KeyBoardControl();
}
Bullet_DeltaTime += Time.deltaTime;
if (Bullet_DeltaTime > Bullet_Delay) {
Instantiate (bullet, this.gameObject.transform.position, Quaternion.identity);
Bullet_DeltaTime = 0.0f;
}
}
void KeyBoardControl(){
// 캐릭터 좌우 이동, 화면 밖으로 나갔을때 처리
float dirX = Input.GetAxis ("Horizontal");
this.gameObject.transform.Translate (Vector3.right * dirX * speed*Time.deltaTime);
// 캐릭터가 화면 왼쪽으로 나갔을때 X 최소값으로 되돌린다.
if (this.gameObject.transform.position.x < -Screen_Size.x+(Player_SpriteSize.x)) {
this.gameObject.transform.position = new Vector3(-Screen_Size.x+(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터가 화면 오른쪽으로 나갔을때 X 최대값으로 되돌린다.
else if(this.gameObject.transform.position.x > Screen_Size.x-(Player_SpriteSize.x)){
this.gameObject.transform.position = new Vector3(Screen_Size.x-(Player_SpriteSize.x),this.gameObject.transform.position.y, this.gameObject.transform.position.z);
}
// 캐릭터 상하 이동, 화면 밖으로 나갔을때 처리
float dirY = Input.GetAxis ("Vertical");
this.gameObject.transform.Translate (Vector3.up * dirY * speed*Time.deltaTime);
// 캐릭터가 화면 아래로 나갔을때 Y최소값으로 되돌린다.
if (this.gameObject.transform.position.y < -Screen_Size.y+(Player_SpriteSize.y)) {
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, -Screen_Size.y+(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
// 캐릭터가 화면 아래로 나갔을때 Y최대값으로 되돌린다.
else if(this.gameObject.transform.position.y > Screen_Size.y-(Player_SpriteSize.y)){
this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, Screen_Size.y-(Player_SpriteSize.y), this.gameObject.transform.position.z);
}
}
}