유니티
04/13 유니티 쿠키런 제작 1
박준희
2021. 4. 13. 14:34
배경 이동
캐릭터 점프, 슬라이드 구현
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public float jumpForce = 700f;
private int jumpCount = 0;
private bool isGrounded = false;
private bool isDoubleJump = false;
private bool isSlide = false;
private Rigidbody2D playerRigidbody;
private Animator animator;
private BoxCollider2D[] colliders;
public Button btnJump;
public Button btnSlide;
// Start is called before the first frame update
void Start()
{
this.playerRigidbody = this.GetComponent<Rigidbody2D>();
this.animator = this.GetComponent<Animator>();
this.colliders = this.GetComponents<BoxCollider2D>();
this.btnJump.onClick.AddListener(() =>
{
if (this.jumpCount < 2)
{
this.jumpCount++;
playerRigidbody.velocity = Vector2.zero;
this.playerRigidbody.AddForce(new Vector2(0, this.jumpForce));
if (this.jumpCount > 1)
{
this.isDoubleJump = true;
}
}
else if (this.playerRigidbody.velocity.y > 0)
{
this.playerRigidbody.velocity = this.playerRigidbody.velocity * 0.5f;
}
});
this.btnSlide.onClick.AddListener(() =>
{
});
}
// Update is called once per frame
void Update()
{
this.animator.SetBool("DoubleJump", this.isDoubleJump);
this.animator.SetBool("Grounded", this.isGrounded);
this.animator.SetBool("Slide", this.isSlide);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.contacts[0].normal.y > 0.7f)
{
this.isGrounded = true;
this.isDoubleJump = false;
this.jumpCount = 0;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if(this.transform.position.y > 0.1f)
{
isGrounded = false;
}
}
public void SlideButtonDown()
{
colliders[0].enabled = false;
colliders[1].enabled = true;
isSlide = true;
}
public void SlideButtonUp()
{
colliders[0].enabled = true;
colliders[1].enabled = false;
isSlide = false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BackgroundLoop : MonoBehaviour
{
public float width = 27;
private void Awake()
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (this.transform.position.x <= -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 ScrollingObject : MonoBehaviour
{
public float speed = 10f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
this.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundLoop : MonoBehaviour
{
private float width;
private void Awake()
{
BoxCollider2D backgroundCollider = this.GetComponent<BoxCollider2D>();
this.width = backgroundCollider.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 <= -width)
{
this.Reposition();
}
}
private void Reposition()
{
Vector2 offset = new Vector2(this.width * 2, 0);
this.transform.position = (Vector2)this.transform.position + offset;
}
}
참고
twoicefish-secu.tistory.com/10
유니티 UI 버튼(Button) 기초 및 OnClick 기능 구현
참고 동영상 - Live Training 5th September 2016 - Creating a Main Menu 버튼 생성 Hierarchy 창에서 오른쪽 마우스 버튼을 눌러줍니다. UI -> Button 클릭하면 버튼이 생성됩니다. 버튼 내용 수정 버튼은..
twoicefish-secu.tistory.com
[Unity3D] Parameters 값 읽기 및 쓰기
Parameters 값 읽기 및 쓰기 Parameters의 있는 Bool, Int, Float 값을 읽어오거나 쓰는 방법입니다. 예) Animator.SetInt("Parameters name", 값); -> Animator에서 정의한 Parameters의 값을 수정할 수..
sesok808.tistory.com