유니티 공부하기6 게임 만들기
Rotate(Vector3) : 매개변수 기준으로 회전시키는 함수
어떠한 컴퓨터든 어떠한 환경에든 같은 속도 적용 -> deltaTime
로컬좌표계 transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
월드좌표계 transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime, Space.world);
여기서 up은 y축
오버로드 : 이름은 같지만 매개별수가 다른 함수를 호출.
물리적 충돌 필요 없을시 Is Trigger 체크
SetActive(bool) : 오브젝트 활성화 함수
Audio Source: 사운드 재생 컴포턴트, Audio Cilp 필요
에셋스토어 : 게임 개발에 유용한 애셋들을 판매하는 스토어
Window->Asset Store Ctrl+9 -> 무료 사운드 다운로드
play On Awake : 시작할때 재생 시킨다는 의미.. 충돌할때 마다 사운드 재생시 이것 해제
Update()이후에 실행되는 LateUpdate()
FindGameObjectWithTag():주어진 태그로 오브젝트 검색
주의 FindGameObjectWithTag() , FindGameObjectsWithTag()
여기서 Object와 Objects를 주의!! 삽질함
형태가 없고 전반적인 로직을 가진 오브젝트를 매니저로 지정
Find 계열 함수는 부하를 초래할 수 있으므로 피하는 것을 권장
게임 장면 이동 할때 using UnityEngine.SceneManagerment; 추가
LoadScene(): 주어진 장면을 불러오는 함수
Scene을 부러오려면 꼭 Build Setting 에서 추가할것
UI를 사용하려면 UnityEngine.UI라이브러리 적용 필수
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
Transform playerTransform;
Vector3 Offset;
// Start is called before the first frame update
void Awake()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;;
Offset = transform.position - playerTransform.position;
}
// Update is called once per frame
void Update()
{
transform.position = playerTransform.position + Offset;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int TotalItemCount;
public int stage;
public Text stageCountText;
public Text playerCountText;
private void Awake()
{
stageCountText.text = "/ " + TotalItemCount;
}
public void GetItem(int count)
{
playerCountText.text = count.ToString();
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
SceneManager.LoadScene(stage);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public float rotateSpeed;
void Update()
{
transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime, Space.World);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerBall : MonoBehaviour
{
public float jumpPower;
public int itemCount;
public GameManager manager;
bool isJump;
Rigidbody rigid;
AudioSource audio;
private void Awake()
{
isJump = false;
rigid = GetComponent<Rigidbody>();
audio = GetComponent<AudioSource>();
}
private void Update()
{
if(Input.GetButtonDown("Jump") && !isJump)
{
isJump = true;
rigid.AddForce(new Vector3(0, jumpPower, 0), ForceMode.Impulse);
}
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
rigid.AddForce(new Vector3(h, 0, v),ForceMode.Impulse);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Floor")
isJump = false;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Item")
{
itemCount++;
audio.Play();
other.gameObject.SetActive(false);
manager.GetItem(itemCount);
}
else if (other.tag == "Finish")
{
if(itemCount == manager.TotalItemCount)
{
//Game Clear & next stage
if (manager.stage == 1)
SceneManager.LoadScene(0);
else
SceneManager.LoadScene(manager.stage+1);
}
else
{
//Restart
SceneManager.LoadScene(manager.stage);
}
}
}
}