언어/유니티

4-점프맵만들기

깡 딱 2023. 2. 15. 14:34
728x90

 

background를 Order in Layer -1로 해주고 구름을 입맛에 맞게 배치

깃발을 마지막 맨 위쪽에 위치하도록 만들어줍시다.

 

구름을 배치하기전에 Body Type을 운동학을 바꿔주세요!

그리고 BOX Collider 2D 사이즈를 1.4 0.5로

그리고 고양이에게는 캡슐콜라이더나 고양이 사이즈에 맞게 콜라이더 넣어주세요

 

다음 애니메이션을 만들어줄건데 

애니메이션을 넣을 player 클릭 -> window -> 애니메이션 

walk_cat라는 폴더 하나 생성 후 Sprite 를추가해주고 수정

점프도 마찬가지 

그림이 몇개나 있는지도 중요하고 

(한 파일안에 그림을 다 안 넣으면 용량이 커짐)

 

애니메이션은 이런식 

float 형식으로 할 수도 있지만

여기는 Tigger 형식  

그래서 코드도 그런식으로 적어야 됨

그리고 Has Exit time 거야 됨

 

 

 

Player 코드이다.(자세한 건 전부다 주석)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rigid2D;
    Animator animator;

    float jumpForce = 780.0f;
    float walkForce = 30.0f;
    float maxWalkSpeed = 2.0f;
    void Start()
    {
        this.rigid2D= GetComponent<Rigidbody2D>();
        this.animator= GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && this.rigid2D.velocity.y == 0)
        { //무한점프를 막기위해서 스패이스 바를 눌렀을때와  y축이 0일때 즉 뛰고 있는 상태일때
            this.animator.SetTrigger("JumpTrigger");
            //점프할떄 점프모션을 부름SetTrigger라는 메서드가 필요함
            this.rigid2D.AddForce(transform.up * this.jumpForce);
            // 위쪽*점프힘 만큼 중력에 힘을 받는다.
        }

        int key = 0; //가만이 있을때 0
        if (Input.GetKey(KeyCode.RightArrow)) key = 1; // 오른쪽 화살표 누를시 1
        if (Input.GetKey(KeyCode.LeftArrow)) key = -1;// 오른쪽 화살표 누를시 -1
        //여기 1 -1 숫자는 좌표를 뜻하는거임 왼쪽 오른쪽


        float speedx = Mathf.Abs(this.rigid2D.velocity.x);
        // 데이터 절대값 반환 (Mathf.Abs) ex 100넣으면 -100나오는거
        // speedx가 100도 되고 -100 된다라는뜻 결론으로 player 속도다
        if (speedx < this.maxWalkSpeed)
        { //플레이어 속도는 maxWalkSpeed를 넘지않는다.
            this.rigid2D.AddForce(transform.right * key * this.walkForce);
        } // 오른쪽으로 key가 왼쪽오른쪽을 알려주고 walkForce가 최대값으로 힘을받음
        // 결론은 -30 30 이 최고 속력이 되는거임

        if (key != 0)
        { //key가 0이아닐떄 (-1 1 )
            transform.localScale = new Vector3(key, 1, 1);
        } //이내용은 블로그에 그림참고
        if (this.rigid2D.velocity.y == 0) //y축이 0으로 힘을받을때
        { 
            this.animator.speed = speedx / 2.0f;
        } //플레이어 속도에 맞춰 속도를 바꾼다. 
        else
        {
            this.animator.speed = 1.0f;
        } //점프를 안하면 1.0속도로~

     

        if(transform.position.y < -10)
        { //만약임마가 떨어진다 gameScene불러옴
            SceneManager.LoadScene("gameScene");
        }
      

    }
    void OnTriggerEnter2D(Collider2D other) //오브젝트간 충돌에서 벗어날때 한번 호출되는 함수.
    {  //한마디로 설명하면 깃발에는 Trgger를 켜놔서 통과가되는데 만약 통과하면 골이뜨고 씬이동
        Debug.Log("골");
        SceneManager.LoadScene("ClearScene");
    }
}

 

 

 

 

 

 

아까 좌우반전에 대한 내용이다 참고하면 된다.

이 방법도 있고 Filp을 껐다 켰다 해주는 방법도 있어서 자기가 맞는 방식의 게임을 개발하면 될 거 같다.

 

 

그리고 메인 카메라가 player를 따라가야 되기 때문에

카메라에 이 코드 넣어주세요

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    GameObject player;

    void Start()
    {
        this.player = GameObject.Find("cat"); //게임오브젝트에서 cat를 찾아서 player칭함
    }
    // Update is called once per frame
    void Update()
    {
        Vector3 playerPos = this.player.transform.position;
            transform.position=new Vector3(transform.position.x,playerPos.y, transform.position.z);

        //x z축은 나두고  player의 y축이 움직였을때  playerpos로 저장되면 업데이트됨
    }
}

 

 

 

그리고 씬 한 개더 만들어주고

 

이거 넣고 여기에도 코드를

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CleraDirector : MonoBehaviour
{

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadScene("gameScene");
        }//클릭하면 게임 씬으로 돌아감
    }
}

 

이거까지 넣으면 아주 아주 간단한 점프게임을 만들 수있게 된다.

 

 

여기서 스마트폰을 빌드 하고 싶다면  이렇게 점프랑 움직임만 

수정해주면된다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rigid2D;
    Animator animator;

    float jumpForce = 780.0f;
    float walkForce = 30.0f;
    float maxWalkSpeed = 2.0f;
    float threshold = 0.2f; //함수 선언 
    void Start()
    {
        this.rigid2D= GetComponent<Rigidbody2D>();
        this.animator= GetComponent<Animator>();
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0)&&this.rigid2D.velocity.y==0)
        { //손가락으로 눌렀을떄
            this.rigid2D.AddForce(transform.up * this.jumpForce);
        }

        int key = 0; 
        if (Input.acceleration.x > this.threshold) key = 1; 
        if (Input.acceleration.x < -this.threshold) key = -1;
        //acceleration 기울기 관련 함수


        float speedx = Mathf.Abs(this.rigid2D.velocity.x);
        if (speedx < this.maxWalkSpeed)
        { 
            this.rigid2D.AddForce(transform.right * key * this.walkForce);
        } 

        if (key != 0)
        { 
            transform.localScale = new Vector3(key, 1, 1);
        } 
        if (this.rigid2D.velocity.y == 0) 
        { 
            this.animator.speed = speedx / 2.0f;
        } 
        else
        {
            this.animator.speed = 1.0f;
        } 

     

        if(transform.position.y < -10)
        { 
            SceneManager.LoadScene("gameScene");
        }
      

    }
    void OnTriggerEnter2D(Collider2D other)
    {  
        Debug.Log("골");
        SceneManager.LoadScene("ClearScene");
    }
}

 

 


갤럭시s22 울트라에서 구동한 모습이다.

잘 돌아간다.

근데 델타타임을 넣어야 될거같다. 

기종이나 컴퓨터랑 차이가 좀 나는거같다.

728x90