Udemy 강좌 'Complete C# Unity Game Developer 3D' 리뷰 및 기술 기록지입니다.
Obstacle Course : 18.Introduction to methods ~ 30. Wrap Up - Obstacle Course
18.Introduction to methods
함수 호출하는 방법에 대해 알아보았다.
메소드를 호출할 때 : 리턴받을 정보를 물어보고, 파라미터가 필요하다면 명시할 수 있다.
메소드 선언시 return value, function name, parmater( ()안에 쓰기) 순으로 나열 뒤 코드 블록 { } 안에 자세한 내용을 적으면 된다.
메소드를 호출할 때는 '함수명 () 형식'으로 호출하면 된다.
start() & update() 이 경우 유니티의 내장함수이다.
19. Practicing with methods
실제로 메소드 구현하는 실습하는 단계이다.
Mission _ PrintInstruction 을 호출해보기 : Start()에서는 시작할 때 호출이 되므로 start() 메소드 안에 넣어주었다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
float moveSpeed = 10f;
// Start is called before the first frame update
//type whatever you like
void Start()
{
PrintInstruction();
}
// Update is called once per frame
void Update()
{
MovePlayer();
}
void PrintInstruction(){
Debug.Log("Welcome to the game");
Debug.Log("Move your player with WASD or arrow Keys ");
Debug.Log("Don't hit the walls!");
}
void MovePlayer(){
float xValue = Input.GetAxis("Horizontal")*Time.deltaTime*moveSpeed;
float zValue = Input.GetAxis("Vertical")*Time.deltaTime*moveSpeed;
transform.Translate(xValue,0,zValue);
}
}
20. Using onCollisionEnter()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHit : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
Debug.Log("Bumped into a wall")
}
}
onColiisionEnter(Collision Enter) 메소드는 충돌을 감지해주는 메소드이다. 충돌을 감지해 실행이 되었을 때 다음과 같이
콘솔창에 메세지를 남길 수 있도록 했다.
<결과>
console 창 메세지
21. Using GetComponent<>
GetComponent에 <> 안에 색 변화를 담당하는 MeshRenderer를 가져와 그 색깔을 바꿀 수 있게 했다.
충돌을 감지했을 때 빨간색으로 변화하도록 Color 메소드의 속성 red를 적용시켰다.
private void OnCollisionEnter(Collision other)
{
Debug.Log("Bumped into a wall");
GetComponent<MeshRenderer>().material.color = Color.red;
}
}
<결과>
부딪혔을 때 빨간색으로 바뀜
22. Incrementing a Score
간단히 문법을 이용해 충돌을 감지했을 경우 hit 변수 선언을 해 몇번 충돌을 감지했는지 콘솔창에 출력되도록 했다.
public class Scorer : MonoBehaviour
{
int hits = 0;
private void OnCollisionEnter(Collision other)
{
hits++;
Debug.Log("You've bumped into a thing this many times" + hits);
}
}
<결과>
23. Using Time
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
MeshRenderer renderer; //caching a reference
Rigidbody rigidbody;
[SerializeField] float timeToWait = 3f;
void Start()
{
renderer = GetComponent<MeshRenderer>();
rigidbody = GetComponent<Rigidbody>();
renderer.enabled = false;
rigidbody.useGravity = false;
}
// Update is called once per frame
void Update()
{
if (Time.time > timeToWait){
renderer.enabled = true;
rigidbody. useGravity = true;
}
}
}
24. If statements
25. Caching A reference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Dropper : MonoBehaviour
{
MeshRenderer renderer; //caching a reference
Rigidbody rigidbody;
[SerializeField] float timeToWait = 3f;
void Start()
{
renderer = GetComponent<MeshRenderer>();
rigidbody = GetComponent<Rigidbody>();
renderer.enabled = false;
rigidbody.useGravity = false;
}
// Update is called once per frame
void Update()
{
if (Time.time > timeToWait){
renderer.enabled = true;
rigidbody. useGravity = true;
}
}
}
26. Using Tags
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectHit : MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag=="Player"){
GetComponent<MeshRenderer>().material.color = Color.red;
gameObject.tag ="Hit";
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scorer : MonoBehaviour
{
int hits = 0;
private void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag != "Hit")
{
hits++;
Debug.Log("You've bumped into a thing this many times" + hits);
}
}
}
27. Rotate An Object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spinner : MonoBehaviour
{
[SerializeField] float xAngle = 0;
[SerializeField] float yAngle = 0;
[SerializeField] float zAngle = 0;
void Update()
{
transform.Rotate(xAngle,yAngle,zAngle);
}
}
28. Prepare our prehabs
29. Build An Obstacle Course
30. Wrap up