씬 변경
애드포스는 점프, 벨로시티는 자연스러운 움직임 -1~1, 트랜슬래이트는 위치 변경
깃발의 피봇을 변경해 닿으면 돌아가게 함
- jumpCat
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class jumpCat : MonoBehaviour
{
public Animator animator;
Rigidbody2D Rigidbody2D;
void Start()
{
this.animator = GetComponent<Animator>();
this.Rigidbody2D = GetComponent<Rigidbody2D>(); //Constrain-Freeze Z로 넘어지지 않게 함
}
void Update()
{
float h = Input.GetAxisRaw("Horizontal"); // -1~1 부드러운 이동 // 버티컬도 -1~1이라 점프는 안맞음
if (h == 0)
{
animator.SetInteger("State", 0); //Idle, 애니매이터에 State로 설정한 값
}
if (h != 0)
{
animator.SetInteger("State", 1); // 걷기
this.transform.localScale = new Vector3(h, 1, 1);
this.Rigidbody2D.velocity = new Vector2(h * 2, 0); // 마찰에 영향을 받는 일정한 방향, 스피드
}
if (Input.GetKeyDown(KeyCode.Space))
{
// Debug.Log("점");
animator.SetInteger("State", 2);
this.Rigidbody2D.AddForce(transform.up * 450f); // 가속이 붙었다 줄어드는 점프에 적합 읏!샤....
}
}
}
- jumpFlag
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class jumpFlag : MonoBehaviour
{
Rigidbody2D Rigidbody2D;
private void Start()
{
this.Rigidbody2D = GetComponent<Rigidbody2D>();
}
void OnTriggerEnter2D(Collider2D collider) // if 트리거가 적용된 콜라이더가 닿는다면 과 같음
{
StartCoroutine(FlagSpin());
}
IEnumerator FlagSpin()
{
// 스프라이트 반전 5번
for(int i =0; i < 5; i++)
{
this.transform.localScale = new Vector3(-1, 1, 1); // 스프라이트 피봇 위치 변경
yield return new WaitForSeconds(0.15f);
this.transform.localScale = new Vector3(1, 1, 1);
yield return new WaitForSeconds(0.15f);
// Debug.Log(i);
}
SceneManager.LoadScene("gameover");
yield return null;
}
}
- Tab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Tab : MonoBehaviour
{
void Update()
{
// 탭 누르면 게임 신으로 이동
if (Input.GetKeyDown(KeyCode.Tab))
{
SceneManager.LoadScene("jump");
}
}
}
빌드에 사용할 씬을 등록해야 하며
스타트씬은 0번으로 끌어놓는다.
'└▶연습' 카테고리의 다른 글
밤송이2: 스크린 좌표 -> 월드 좌표 (0) | 2024.09.17 |
---|---|
밤송이 던지기1 (0) | 2024.09.17 |
고양이 화살 피하기 3단계 (6) | 2024.09.16 |
고양이 화살 피하기 2단계 (1) | 2024.09.16 |
고양이 화살 피하기 1단계 (2) | 2024.09.15 |