player
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
public class player : MonoBehaviour
{
public Rigidbody2D rb; // 공(플레이어)에 붙어있는 Rigidbody2D를 넣어줄 변수 (물리/속도 담당)
public float moceSpead = 1; // 좌우로 굴러가는 속도(스피드) ※ 오타지만 변수명이라 동작은 함
public float jumpForce = 1; // 바닥에 닿을 때 위로 튀는 힘(점프 힘)
public GameObject panel;
public List<GameObject> stars = new List<GameObject>();
void Update()
{
float h = Input.GetAxis("Horizontal");
rb.linearVelocityX = h * moceSpead;
if(transform.position.y < -10) //밑으로 떨어지면 재시작
{
rb.linearVelocity = Vector2.zero; // 높이 초기화 = 이걸 안하면 떨어지고 재시작할때 가속도가 붙어서 많이 튕긴다
transform.position = new Vector3(-18.4f, -1.32f, 0);
}
}
// 물리 기반 코드는 FixedUpdate 에서 실행 하는게 좋다
private void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
rb.linearVelocityX = h * moceSpead;
if (transform.position.y < -10) //밑으로 떨어지면 재시작
{
rb.linearVelocity = Vector2.zero; // 높이 초기화 = 이걸 안하면 떨어지고 재시작할때 가속도가 붙어서 많이 튕긴다
transform.position = new Vector3(-18.4f, -1.32f, 0);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("enemy")) // 뾰족 상자에 닿음녀 동작
{
transform.position = new Vector3(-18.4f, -1.32f, 0); // 원래 자리로
}else if (collision.gameObject.CompareTag("Finish")) // 별에 닿으면 동작
{
int index = Array.IndexOf(stars.ToArray(), collision.gameObject); // 몇번쨰 별인지
stars.Remove(stars[index]);
Destroy(collision.gameObject); // 별이 사라진다
if (stars.Count == 0) // 별이 다 사라졌는지
{
Time.timeScale = 0; // 시간의 속도 0은 시간 정지
panel.SetActive(true);
}
}
else
{
rb.AddForceY(1 * jumpForce);
}
}
}
화면 구성
using UnityEngine;
using UnityEngine.SceneManagement;
public class LobbyManager : MonoBehaviour
{
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GameStart()
{
SceneManager.LoadScene(0); // 0번 화면으로 변경
}
public void easter()
{
SceneManager.LoadScene(2); // 2 화면으로 변경
}
}