반응형
SMALL
Cat Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Cat : MonoBehaviour
{
CircleCollider2D circleCollider;
Animator anim;
Rigidbody2D rBody2D;
public float jumpForce;
public float walkfForce;
public float walkfSpeed;
public bool isClear;
int jumpCnt;
// Start is called before the first frame update
void Start()
{
this.rBody2D = GetComponent<Rigidbody2D>();
this.anim = GetComponent<Animator>();
this.circleCollider = GetComponent<CircleCollider2D>();
}
// Update is called once per frame
void Update()
{
if (this.transform.position.y < -10f) SceneManager.LoadScene("Gamescene");
//점프
if (Input.GetKeyDown(KeyCode.Space) && this.jumpCnt < 2)
{
this.anim.SetTrigger("JumpTrigger");
this.rBody2D.AddForce(this.transform.up * jumpForce);
jumpCnt++;
}
if (this.jumpCnt == 2 && this.rBody2D.velocity.y ==0) { this.jumpCnt = 0;}
//좌우 이동
int dir = 0;
if (Input.GetKey(KeyCode.LeftArrow)) { dir = -1; this.GetComponent<SpriteRenderer>().flipX = true; };
if(Input.GetKey(KeyCode.RightArrow)) { dir = 1; this.GetComponent<SpriteRenderer>().flipX = false; };
float speedx = Mathf.Abs(this.rBody2D.velocity.x);
//좌우 이동 속도 제한
if(speedx < this.walkfSpeed) { this.rBody2D.AddForce(transform.right*dir*this.walkfForce); }
//if(dir != 0)
//{
// transform.localScale = new Vector3(dir,1,1);
//}
if (this.rBody2D.velocity.y == 0)
this.anim.speed = speedx / 2.0f;
else
this.anim.speed = 3.0f;
}
public void OnTriggerEnter2D(Collider2D collision)
{
Debug.LogFormat("collision 메서드 호출 : {0}", collision);
isClear = true;
}
}
Camera Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour
{
public GameObject cat;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var catPos = cat.transform.position;
if(catPos.y < 0)
{
catPos.y = 0;
}
this.transform.position = new Vector3(this.transform.position.x, catPos.y, this.transform.position.z);
}
}
ClaerDirector Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ClearDirector : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
SceneManager.LoadScene("Gamescene");
}
}
}
Game Director Script
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameDirector : MonoBehaviour
{
GameObject cat;
// Start is called before the first frame update
void Start()
{
this.cat = GameObject.Find("Cat");
}
// Update is called once per frame
void Update()
{
if(cat.GetComponent<Cat>().isClear)
{
SceneManager.LoadScene("ClearScene");
cat.GetComponent<Cat>().isClear = false;
}
}
}
구현사항
- Scene 전환 (클리어 Scene , 게임 Scene )
- Out of MainCamera 되었을시 ScencManager의 LoadScene 을 이용하여 다시 게임 씬 불러오기
- 2단점프 (구름에 끼임 버그를 포함한 약간의 버그 있음)
- MainCamera 플레이어를 따라 이동
- 좌우 이동 및 점프시 캐릭터 애니메이션 변경 (점프모션 전환 포함 약간의 수정필요)
- OnTriggerEnter2D 이벤트 메서드를 사용하여 충돌 이벤트 발생 구현
- SetTrigger 메서드를 이용한 애니메이션 전환
PS.누가 만든 게임인지는 몰라도 조작감이 정말 구리다
반응형
LIST
'유니티 엔진 클라이언트 > 개인공부' 카테고리의 다른 글
| [유니티] UI 스크롤 기능 구현 및 변하지 않는데이터 직렬화 역직렬화 (0) | 2023.02.13 |
|---|---|
| [유니티] 종스크롤 2D 슈터 제작 (2) | 2023.02.10 |
| [유니티] UI 팝업 & 체크박스 &버튼 연계 연습 (0) | 2023.02.09 |
| [유니티] 종스크롤 스페이스 슈터 (0) | 2023.02.02 |
| [유니티] 충돌 판정 구현 및 UI 업데이트 복습 (0) | 2023.01.30 |