0412 쿠키런 달리기
2021. 4. 12. 18:22ㆍunity
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//점프 힘
public float jumpForce = 700f;
//누적 점프 횟수
private int jumpCount = 0;
//바닥에 닿았는지 나타냄
private bool isGrounded = false;
//사망 상태
private bool isDie = false;
//사용할 리지드바디 컴포넌트
private Rigidbody2D playerRigidbody;
//사용할 애니메이터 컴포넌트
private Animator animator;
void Start()
{
//초기화
//컴포넌트 가져오기
this.playerRigidbody = this.GetComponent<Rigidbody2D>();
this.animator = this.GetComponent<Animator>();
}
void Update()
{
if (this.isDie) return;
//사용자 입력을 감지하고 점프하는 처리
//마우스 왼쪽 버튼을 눌렀고 최대 점프 횟수 (2)에 도달하지 않았다면
if (Input.GetMouseButtonDown(0) && this.jumpCount < 2)
{
//점프 횟수 증가
this.jumpCount++;
//점프 직전에 속도를 순간적으로 0,0으로 변경
playerRigidbody.velocity = Vector2.zero;
//리지드바디에 위쪽 힘주기
this.playerRigidbody.AddForce(new Vector2(0, this.jumpForce));
//오디오 소스 재생
}
else if (Input.GetMouseButtonUp(0) && this.playerRigidbody.velocity.y > 0)
{
//마우스 왼쪽 버튼에서 손을 떼었고 y축의 속도가 양수라면 (위로 상승중이라면)
//현재 속도를 절반으로
this.playerRigidbody.velocity = this.playerRigidbody.velocity * 0.5f;
}
//애니메이터의 Grounded파라메터를 isGrounded값을 생신 (애니메이션 플레이)
this.animator.SetBool("Grounded", this.isGrounded);
}
private void Die()
{
//사망처리
//애니메이터의 Die 트리거 파라메터 설정
this.animator.SetTrigger("Die");
//속도를 0으로 변경
this.playerRigidbody.velocity = Vector2.zero;
//사망 상태 변경
this.isDie = true;
}
private void OnTriggerEnter2D(Collider2D collision)
{
//트리거 콜라이더를 가진 장애물과의 충돌감지
if (collision.tag == "Dead" && !this.isDie)
{
//충돌한 상대방의 태그가 Dead고 아직 죽지 않았다면
this.Die();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
//바닥에 닿았음을 감지 하는 처리
//어떤 콜라이더와 닿았으며 충돌 표면이 위쪽을 보고있으면
if (collision.contacts[0].normal.y > 0.7f)
{
//isGrounded를 true로 변경 누적점프 횟수를 0으로 리셋
this.isGrounded = true;
this.jumpCount = 0;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
//바닥에서 벗어났음을 감지하는 처리
isGrounded = false;
}
using UnityEngine;
public class BackgroundLoop : MonoBehaviour
{
private float width; //배경의 가로 길이
//객체가 인스턴화 되었을때 한번
private void Awake()
{
//가로 길이를 측정하는 처리
BoxCollider2D backgroundCollider = this.GetComponent<BoxCollider2D>();
this.width = backgroundCollider.size.x;
}
// Update is called once per frame
void Update()
{
//현재 위치가 원점에서 왼쪽으로 width이상 이동했을때 위치를 재배치
if (this.transform.position.x <= -width)
{
this.Reposition();
}
}
//위치 재배치 메서드
private void Reposition()
{
Vector2 offset = new Vector2(this.width * 1f, 0);
this.transform.position = (Vector2)this.transform.position + offset;
}
}
using UnityEngine;
//게임 오브젝트를 계속 왼쪽으로 움직이는 스크립트
public class ScrollingObject : MonoBehaviour
{
public float speed = 1f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//게임 오브젝트를 일정 속도로 왼쪽으로 평행이동
//초딩 speed의 속도로 왼쪽으로 이동
this.transform.Translate(Vector2.left * speed * Time.deltaTime);
}
}
using UnityEngine;
using UnityEngine.UI;
public class JumpButton : MonoBehaviour
{
public Animator anim;
private Rigidbody2D playerRigidbody;
private bool isGrounded;
// Start is called before the first frame update
private void Start()
{
this.playerRigidbody = this.GetComponent<Rigidbody2D>();
this.anim = this.GetComponent<Animator>();
}
public void Jump()
{
if (Input.GetMouseButtonUp(0) && this.playerRigidbody.velocity.y > 0)
{
this.anim.SetBool("Grounded", this.isGrounded);
}
}
}
버튼 클릭 시 점프 및 아이콘 호버 구현
dobby-the-house-elf.tistory.com/62
[Unity/C#] UI에 마우스 이벤트 적용 시키기
Unity에서 기본적으로 제공하는 마우스 이벤트에 대한 콜백함수는 다음과 같다. 1. OnMouseDown - 오브젝트 위에서 마우스 왼쪽 버튼이 눌렸을 때 호출 2. OnMouseDrag - OnMouseDown이 일어나고 마우스 버튼
dobby-the-house-elf.tistory.com
cookierun_rogue.unitypackage
0.71MB
Mobile - Cookie Run OvenBreak - HUD.zip
0.74MB
Mobile - Cookie Run OvenBreak - Roguefort Cookie.zip
3.97MB
'unity' 카테고리의 다른 글
0415 코루틴 사용해서 자동총쏘기 구현+코루틴 연습(fadeout) (0) | 2021.04.15 |
---|---|
0414 cinemachine/coroutine/IK pivot (0) | 2021.04.14 |
0409 캐릭터의 시야각과 공격범위 생성 (0) | 2021.04.09 |
0409 총알 피하기 게임 (랜덤 총알 + 유도탄 생성) (0) | 2021.04.09 |
0408 마우스 클릭하는 곳으로 캐릭터 이동시키기 (0) | 2021.04.08 |