Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

브래의 슬기로운 코딩 생활

모바일 게임 개발 기말고사 정리 본문

2-1/모바일 게임 개발

모바일 게임 개발 기말고사 정리

김브래 2023. 6. 21. 22:58

Addforce()

- rigidbody에 기반한 힘

- =가속도=속도의 변화량이기 때문에 속도라기보다는 속도 변화에 대한 값

Rigidbody.velocity: Rigidbody의 속도벡터

Rigidbody.Addforce(방향 * )=> 가속도()을 지정할 수 있는 속성

 

Rigidbody2D rigid2D;

void Start() {

this.rigid2D = GetComponent<Rigidbody2D>(); // Rigidbody2D부여

}

 

if(Input.GetKeyDown(KeyCode.Space) && this.rigid2D.velocity.y == 0) {

// Y축으로 움직이지 않고 스페이스바를 누르면

this.rigid2D.AddForce(transform.up * this.jumpForce);

// 점프한다

Rigidbody.velocity: 움직이는 객체의 속도를 알아낼 수 있는 속성(벡터)

 

float speedx = Mathf.Abs(this.rigid2D.velocity.x);

// 플레이어 속도를 절대값으로 변경

if(speedx < this.maxWalkSpeed) { // 스피드 제한

this.rigid2D.AddForce(transform.right * key * this.walkForce);

// X축으로 움직이는 속도가 최대 속도(maxWalkSpeed)보다 빠르면 움직이지 않음 X축으로 움직이는 속도가 최대 속도 (maxWalkSpeed) 보다 느리면 움직임

 

if(key != 0) { // 플레이어의 방향 바꾸기

transform.localScale = new Vector3(key, 1, 1);

 

씬 전환

 

using UnityEngine.SceneManagement; //LoadScene을 사용하기 위하여 필수

SceneManager.LoadScene("GameScene"); // GameScene을 불러온다.

 

void OnTriggerEnter2D(Collider2D other) // 다른 객체와 충돌했을 때 호출되는 함수, other가 충돌한 객체

{

Debug.Log("");

SceneManager.LoadScene("ClearScene");

}

 

카메라 이동

 

GameObject player;

void Start() {

this.player = GameObject.Find("cat"); // 플레이어를 찾음

}

void Update() {

Vector3 playerPos = this.player.transform.position;

transform.position = new Vector3(transform.position.x, playerPos.y, transform.position.z);

// 카메라와 플레이어의 Y축 위치가 같아짐

 

 

 

화살표 프리펩 복제

 

public GameObject arrowPrefab // 화살표 프리펩과 연결

void Update() {

GameObject go = Instantiate(arrowPrefab) as GameObject; // 화살 프리펩 복제

int px = Random.Range(-3, 4);

go.transform.position = new Vector3(px, 7, 0); // -33 사이의 x축에 배치

 

Time.deltaTime = 프레임을 처리하는데 걸리는 시간

 

float delta = 0;

void Update() {

this.delta += Time.deltaTime;

if(this.delta > 1.0) { // 1초 마다

this.delta = 0;

 

충돌 판정

 

Vector2 p1 = transform.position; //사과의 중심좌표

Vector2 p2 = this.player.transform.position; // 고양이의 중심좌표

Vector2 dir = p1 - p2;

float d = dir.magnitude;//사과와 고양이 사이의 거리

float r1 = 0.5f; // 사과의 반지름

Float r2 = 1.0f; // 고양이의 반지름

If(d < r1 + r2) {

// 충돌하면

Destroy(gameObject);//gameObject(사과)가 사라지는 명령어

GameObject director = GameObject.Find("GameDirector");

director.GetComponent<GameDirector>().DecreaseHp(); // 감독 스크립트의 DecreaseHp 함수 호출.

}

감독 스크립트

GameObject hpGage;

 

void Start(){

this.hpGage = GameObject.Find(“hpGage”); // hpGage를 찾음

}

public void DecreaseHp(){

this.hpGage.GetComponent<image>().fillAmount -= 0.1f; // 채력 이미지가 0.1씩 줄어듦

}

 

Vector3.forward: 앞으로 전진

Vector3.back: 뒤로 후진

Vector3.right: 오른쪽으로 이동

Vector3.left: 왼쪽으로 이동

Vector3.up:위로 이동

Vector3.down:아래로 이동

 

CharacterController컴포넌트

 

controller.Move (currentMovement * Time.deltaTime);

 

바닥인가를 체크

controller.isGrounded->true, false //controller가 바닥에 닿았는가를 체크

if (!controller.isGrounded)

{ currentMovement -= new Vector3(0, 3 * Time.deltaTime, 0); }

 

결승점인가를 체크

void Start()

{ reset_pos = transform.position; } // 시작 위치 저장

void Update()

{

if (transform.position.y < -5 || transform.position.z > 65 ) // 결승점에 도달했거나 떨어지면

{

transform.position = reset_pos;//처음 위치로 이동

currentMovement = new Vector3(0, 0, 0);// 속도 0으로 초기화


객체가 왼쪽, 오른쪽으로 움직이는 원리

transform.Translate (Vector3.right * acc * direction * Time.deltaTime);

 

void Update () {

if (transform.position.x > 10 || transform.position.x < -10 ) {

//만약 x값이 10보다 크거나 -10보다 작으면 direction-1을 곱하여 역방향으로 전환

direction *= -1 ;

}

transform.Translate (Vector3.right * acc * direction * Time.deltaTime);

}

 

카메라가 큐브를 따라다님

Vector3 camera_move;

void Update()

{ //카메라의 위치

camera_move = new Vector3(target.position.x - transform.position.x, 0, target.position.z - (transform.position.z + 10));

// 카메라 새로운 위치 = [Cube가 움직인 x위치-현재 카메라의 x위치, 0 , Cube가 움직인 z축 위치 (현재 카메라의 z축 위치 +10)]

transform.Translate(camera_move, Space.World);//카메라의 위치 이동

transform.LookAt(target);//카메라가 target을 바라 봄

}

 

밤송이 날리기

AddForce

OnCollisionEnter()

Instantiate()

 

void OnCollisionEnter(Collision other) {

GetComponent<Rigidbody>().isKinematic = true;

//isKinematictrue일 경우만 충돌을 감지

 

public GameObject Prefab1;

void Update() {

if(Input.GetMouseButtonDown(0)) {

GameObject bamsongi = Instantiate(Prefab1) as GameObject; // 밤송이 프리펩 복제

ScreenPointToRay()

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

//카메라로부터 입력된 마우스 위치까지 가상의 선을 그음

 

Vector3.normalized 정규화 된 벡터는 방향 값만 갖고 길이는 1이다.

 

public GameObject Prefab1;//

void Update() {

if(Input.GetMouseButtonDown(0)) {

GameObject bamsongi = Instantiate(Prefab1) as GameObject;

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 카메라와 마우스 위치 사이에 가상의 선을 그음

Debug.DrawLine(ray.origin, Input.mousePosition, Color.red, 10.0f);//선을 그림

Vector3 worldDir = ray.direction;

bamsongi.GetComponent<Rigidbody>().AddForce(worldDir.normalized * 2000);

//방향을 정규화하여 힘의 크기를 항상 2000이 되도록 정함

}}}

-밤송이를 날리는 방향은 ScreenPointToRay 메서드를 사용해 스크린위의 점을 3D

좌표계로 계산

=> 마우스를 클릭한 방향으로 밤송이가 발사됨

 

if(Input.GetMouseButtonDown(0)) // 좌클릭 했을 때

if(Input.GetMouseButtonDown(1)) // 우클릭 했을 때