Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- python
- 프로그래밍
- 기초
- c++
- w3school
- Class
- dfs
- Tutorial
- 시작해요 언리얼 2022
- Material
- Unity
- C#
- Basic
- W3Schools
- github
- UE5
- dynamic
- String
- 문제풀이
- parameter
- 오류
- guide
- Algorithm
- Programming
- 백준
- DP
- 파이썬
- 재귀
- loop
- Unreal Engine 5
Archives
- Today
- Total
행복한 개구리
Unity 2D 쿠키런 21.04.12 본문
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackLoop : MonoBehaviour
{
float width;
private void Awake()
{
BoxCollider2D BD = this.GetComponent<BoxCollider2D>();
this.width = BD.size.x;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(this.transform.position.x <= -this.width)
{
this.Reposition();
}
}
private void Reposition()
{
Vector2 offset = new Vector2(this.width * 2, 0);
this.transform.position = (Vector2)this.transform.position + offset;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollObjects : MonoBehaviour
{
public float speed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(this.tag == "Tile")
this.transform.Translate(Vector2.left * 1.2f * this.speed * Time.deltaTime);
this.transform.Translate(Vector2.left * this.speed * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CookieController : MonoBehaviour
{
public AudioClip deathClip;
Rigidbody2D playerRigid2D;
AudioSource playerAudio;
Animator animator;
float jumpForce = 400f;
bool isDie = false;
bool isGrounded = false;
int jumpCount = 0;
bool doubleJump = false;
bool isSlide = false;
int slideCount = 0;
// Start is called before the first frame update
void Start()
{
this.playerRigid2D = this.GetComponent<Rigidbody2D>();
this.animator = this.GetComponent<Animator>();
this.playerAudio = this.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (this.isDie) return;
if(this.slideCount < 1 && Input.GetMouseButton(1))
{
this.isSlide = true;
}
if(this.isSlide && Input.GetMouseButtonUp(1))
{
this.isSlide = false;
}
if (this.jumpCount < 2 && Input.GetMouseButtonDown(0))
{
this.isSlide = false;
this.isGrounded = false;
this.jumpCount++;
if(this.jumpCount == 2)
{
this.doubleJump = true;
}
this.playerRigid2D.velocity = Vector2.zero;
this.playerRigid2D.AddForce(new Vector2(0, this.jumpForce));
}
else if (Input.GetMouseButtonUp(0) && this.playerRigid2D.velocity.y > 0)
{
this.playerRigid2D.velocity = this.playerRigid2D.velocity * 0.75f;
}
this.animator.SetBool("Grounded", this.isGrounded);
this.animator.SetBool("Double", this.doubleJump);
this.animator.SetBool("Slide", this.isSlide);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.contacts[0].normal.y > 0.7f)
{
this.isGrounded = true;
this.jumpCount = 0;
this.doubleJump = false;
}
}
private void Die()
{
this.isDie = true;
this.animator.SetTrigger("Die");
}
}
원래는
private void OnCollisionExit2D(Collision2D collision)
{
this.isGrounded = false;
}
라는 메서드도 있었지만 tiles와 tiles가 이어지는 부분에서 false판정을 받아 점프 애니메이션을 출력하길래 Input쪽에서 this.isGrounded를 false를 받도록 코드를 바꿨다. 문제는 해결됐다. 또한 점프시 슬라이드판정을 false로 바꿔서 슬라이드 중 점ㄴ프를 하면 점프애니메이션이 제대로 나오게 했다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI : MonoBehaviour
{
public Slider HpGauge;
public Text Score;
public Text GameOver;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public UI ui;
float scoreDelta;
int score = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.score = (int)Time.deltaTime;
this.ui.Score.GetComponent<Text>().text = "Score " + this.score;
}
private void CheckScore()
{
}
}
녹화_2021_04_13_13_19_00_569.mp4
6.22MB
============================================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public UI ui;
bool isOver = false;
GameObject stage;
ScrollObjects scrollOb;
Slider hpGauge;
float scoreDelta;
int score = 0;
float delta;
public static GameManager instance;
private void Awake()
{
if(GameManager.instance == null)
{
GameManager.instance = this;
}
else if(GameManager.instance != null)
{
Debug.LogFormat("이미 게임매니저 객체가 존재합니다");
Object.Destroy(this.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
this.stage = GameObject.Find("Stage");
this.scrollOb = this.stage.GetComponent<ScrollObjects>();
this.hpGauge = GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
this.ui.Score.GetComponent<Text>().text = "Score " + this.score;
this.CheckScore();
if (this.ui.HpGauge.value <= 0)
{
this.isOver = true;
}
if (this.isOver)
{
this.delta += Time.deltaTime;
if(this.delta > 1.5f)
this.ui.GameOver.gameObject.SetActive(true);
}
}
private void CheckScore()
{
this.scoreDelta += Time.deltaTime;
if (this.scoreDelta > 1.0f)
{
this.score += 100;
}
}
public bool IsOver()
{
return this.isOver;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollObjects : MonoBehaviour
{
public float speed = 5f;
float delta = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!GameManager.instance.IsOver())
{
if (this.tag == "Tile")
this.transform.Translate(Vector2.left * 1.2f * this.speed * Time.deltaTime);
this.transform.Translate(Vector2.left * this.speed * Time.deltaTime);
}
else
{
this.speed *= 0.99f;
this.transform.Translate(Vector2.left * this.speed * Time.deltaTime);
this.delta += Time.deltaTime;
if (this.delta > 1.5f)
this.Pause();
}
}
public void Pause()
{
this.transform.Translate(0, 0, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CookieController : MonoBehaviour
{
public AudioClip deathClip;
Rigidbody2D playerRigid2D;
AudioSource playerAudio;
Animator animator;
float jumpForce = 400f;
bool isDie = false;
bool isGrounded = false;
int jumpCount = 0;
bool doubleJump = false;
bool isSlide = false;
int slideCount = 0;
// Start is called before the first frame update
void Start()
{
this.playerRigid2D = this.GetComponent<Rigidbody2D>();
this.animator = this.GetComponent<Animator>();
this.playerAudio = this.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (this.isDie) return;
if (!GameManager.instance.IsOver())
{
if (this.slideCount < 1 && Input.GetMouseButton(1))
{
this.isSlide = true;
}
if (this.isSlide && Input.GetMouseButtonUp(1))
{
this.isSlide = false;
}
if (this.jumpCount < 2 && Input.GetMouseButtonDown(0))
{
this.isSlide = false;
this.isGrounded = false;
this.jumpCount++;
if (this.jumpCount == 2)
{
this.doubleJump = true;
}
this.playerRigid2D.velocity = Vector2.zero;
this.playerRigid2D.AddForce(new Vector2(0, this.jumpForce));
}
else if (Input.GetMouseButtonUp(0) && this.playerRigid2D.velocity.y > 0)
{
this.playerRigid2D.velocity = this.playerRigid2D.velocity * 0.75f;
}
this.animator.SetBool("Grounded", this.isGrounded);
this.animator.SetBool("Double", this.doubleJump);
this.animator.SetBool("Slide", this.isSlide);
}
else
{
this.Die();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.contacts[0].normal.y > 0.7f)
{
this.isGrounded = true;
this.jumpCount = 0;
this.doubleJump = false;
}
}
private void Die()
{
this.isDie = true;
this.animator.SetTrigger("Die");
this.playerRigid2D.velocity = Vector2.zero;
}
}
SingletonGameManager에 객체를 불러와서 다른 클래스들에 게임오버를 알리려고 했지만 잘 안돼길래 싱글턴을 활용했더니 잘 된다
녹화_2021_04_13_15_55_58_444.mp4
2.77MB
============================================================================
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HpGauge : MonoBehaviour
{
Slider hpGauge;
float hp;
int maxHp= 100;
float delta;
int leftCount;
// Start is called before the first frame update
void Start()
{
this.hpGauge = this.GetComponent<Slider>();
this.hp = this.maxHp;
}
// Update is called once per frame
void Update()
{
this.hpGauge.value = (float)this.hp / this.maxHp;
this.Count();
}
private void Count()
{
this.hp -= Time.deltaTime * 40;
}
public float GetHp()
{
return this.hp;
}
public void SetHp(float count)
{
this.hp = count;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public UI ui;
public static GameManager instance;
bool isOver = false;
GameObject stage;
ScrollObjects scrollOb;
Slider slider;
float scoreDelta;
int score = 0;
float delta;
HpGauge hpGauge;
CookieController cookie;
GameObject ginger;
private void Awake()
{
if(GameManager.instance == null)
{
GameManager.instance = this;
}
else if(GameManager.instance != null)
{
Debug.LogFormat("이미 게임매니저 객체가 존재합니다");
Object.Destroy(this.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
this.ginger = GameObject.Find("Ginger");
this.cookie = this.ginger.GetComponent<CookieController>();
this.hpGauge = GetComponent<HpGauge>();
this.stage = GameObject.Find("Stage");
this.scrollOb = this.stage.GetComponent<ScrollObjects>();
this.slider = GetComponent<Slider>();
/*this.ui.JBtn.onClick.AddListener(() =>
{
this.cookie.Jump();
});
this.ui.SBtn.onClick.AddListener(() =>
{
this.cookie.Slide();
});*/
}
// Update is called once per frame
void Update()
{
this.ui.Score.GetComponent<Text>().text = "Score " + this.score;
if (this.ui.HpGauge.value <= 0)
{
this.isOver = true;
}
else
{
this.CheckScore();
}
}
private void CheckScore()
{
this.scoreDelta += Time.deltaTime;
if (this.scoreDelta > 1.0f)
{
this.scoreDelta = 0;
this.score += 150;
}
}
public void GameOver()
{
if (this.isOver)
{
this.ui.GameOver.gameObject.SetActive(true);
if (Input.anyKeyDown) this.Restart();
}
}
public bool IsOver()
{
return this.isOver;
}
private void Restart()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UI : MonoBehaviour
{
public Slider HpGauge;
public Text Score;
public Text GameOver;
public Button JBtn;
public Button PJBtn;
public Button SBtn;
public Button PSBtn;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CookieController : MonoBehaviour
{
public AudioClip deathClip;
Rigidbody2D playerRigid2D;
AudioSource playerAudio;
Animator animator;
float jumpForce = 400f;
bool isDie = false;
bool isGrounded = false;
int jumpCount = 0;
bool doubleJump = false;
bool isSlide = false;
int slideCount = 0;
// Start is called before the first frame update
void Start()
{
this.playerRigid2D = this.GetComponent<Rigidbody2D>();
this.animator = this.GetComponent<Animator>();
this.playerAudio = this.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (this.isDie) return;
if (!GameManager.instance.IsOver())
{
if (this.slideCount < 1 && Input.GetMouseButton(1))
{
this.slideCount++;
this.isSlide = true;
this.animator.SetBool("Slide", this.isSlide);
}
if (this.isSlide && Input.GetMouseButtonUp(1))
{
this.isSlide = false;
this.animator.SetBool("Slide", this.isSlide);
}
if (this.jumpCount < 2 && Input.GetMouseButtonDown(0))
{
this.isSlide = false;
this.isGrounded = false;
this.jumpCount++;
if (this.jumpCount == 2)
{
this.doubleJump = true;
}
this.playerRigid2D.velocity = Vector2.zero;
this.playerRigid2D.AddForce(new Vector2(0, this.jumpForce));
}
else if (Input.GetMouseButtonUp(0) && this.playerRigid2D.velocity.y > 0)
{
this.playerRigid2D.velocity = this.playerRigid2D.velocity * 0.75f;
}
this.animator.SetBool("Grounded", this.isGrounded);
this.animator.SetBool("Double", this.doubleJump);
this.animator.SetBool("Slide", this.isSlide);
}
if (GameManager.instance.IsOver())
{
GameManager.instance.GameOver();
this.Die();
}
}
/*public void Jump()
{
if (this.jumpCount < 2 && Input.GetMouseButtonDown(0))
{
this.isSlide = false;
this.isGrounded = false;
this.jumpCount++;
if (this.jumpCount == 2)
{
this.doubleJump = true;
}
this.playerRigid2D.velocity = Vector2.zero;
this.playerRigid2D.AddForce(new Vector2(0, this.jumpForce));
}
else if (Input.GetMouseButtonUp(0) && this.playerRigid2D.velocity.y > 0)
{
this.playerRigid2D.velocity = this.playerRigid2D.velocity * 0.75f;
}
this.animator.SetBool("Grounded", this.isGrounded);
this.animator.SetBool("Double", this.doubleJump);
this.animator.SetBool("Slide", this.isSlide);
}
public void Slide()
{
if (this.slideCount < 1 && Input.GetMouseButton(1))
{
this.slideCount++;
this.isSlide = true;
this.animator.SetBool("Slide", this.isSlide);
}
if (this.isSlide && Input.GetMouseButtonUp(1))
{
this.isSlide = false;
this.animator.SetBool("Slide", this.isSlide);
}
}*/
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.contacts[0].normal.y > 0.7f)
{
this.isGrounded = true;
this.jumpCount = 0;
this.doubleJump = false;
}
}
private void Die()
{
this.isDie = true;
this.animator.SetTrigger("Die");
this.playerRigid2D.velocity = Vector2.zero;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollObjects : MonoBehaviour
{
public float speed = 5f;
float delta = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (!GameManager.instance.IsOver())
{
if (this.tag == "Tile")
this.transform.Translate(Vector2.left * 1.2f * this.speed * Time.deltaTime);
this.transform.Translate(Vector2.left * this.speed * Time.deltaTime);
}
else
{
this.speed *= 0.99f;
this.transform.Translate(Vector2.left * this.speed * Time.deltaTime);
this.delta += Time.deltaTime;
if (this.delta > 1.5f)
this.Pause();
}
}
public void Pause()
{
this.transform.Translate(0, 0, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackLoop : MonoBehaviour
{
float width;
private void Awake()
{
BoxCollider2D BD = this.GetComponent<BoxCollider2D>();
this.width = BD.size.x;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(this.transform.position.x <= -this.width)
{
this.Reposition();
}
}
private void Reposition()
{
Vector2 offset = new Vector2(this.width * 2, 0);
this.transform.position = (Vector2)this.transform.position + offset;
}
}
-button&restart 미구현.
'Unity > Project : Unity 2D 쿠키런' 카테고리의 다른 글
Unity 2D 쿠키런 21.04.13 (0) | 2021.04.19 |
---|