Unity-Physics,Rigidbody, 스프라이트 애니메이션, ClimbCloud 예제
2023. 8. 2.

Box2D

 

 

Rigidbody

-물리적 영향을 받아 사실적으로 움직이게함 : 엔진이 움직이게 하는것

-> 물리적 영향을 받으므로 충돌검사를 할 수 있다. : Collider라는 컴포넌트가 필요하다.

 

ClimbCloud 예제에 rigidbody,collider 컴포넌트 추가

 

Cloud의 rigidbody를 kinematic으로 설정

-둘중 하나가 rigidbody가 있어야 충돌검사가 가능하다

collider를 조합해서도 쓸 수 있다.

 

rigidbody 의 freeze rotation으로 z축으로 고양이가 넘어짐을 방지

스프라이트 애니메이션을 만들려면 메카님을 사용해야 한다.

애니메이션 컨트롤러- 애니메이션의 전환을 설정

애니메이터 - 컨트롤러를 참고하여 애니메이션을 최종적으로 실행함

 

cat을 선택한 후 window-Animation-Animation을 누른다.

create animation을 눌러 Animations 폴더에 walk애니메이션을 생성한다.

add property- Sprite -+버튼 - AddKey로 키마다 이미지를 넣어 애니메이션을 만들어준다.

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

public class CatController : MonoBehaviour
{
    private Rigidbody2D rBody2D;
    private float jumpForce = 350f;//점프하는힘
    private float moveForce = 30f;//이동하는힘
    private Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        this.rBody2D = this.GetComponent<Rigidbody2D>();
        this.anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //점프한다.
            this.rBody2D.AddForce(Vector2.up * this.jumpForce);
        }

        int dirX = 0;

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            dirX = -1;
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            dirX = 1;
        }

        float speedX = Mathf.Abs(this.rBody2D.velocity.x);

        if (speedX < 2f)
        {
            // this.rBody2D.AddForce(new Vector2(dirX,0) * this.moveForce);
            this.rBody2D.AddForce(this.transform.right * dirX * this.moveForce);
        }
        //방향에 따라 반전
        if(dirX != 0)
        {
            this.transform.localScale = new Vector3(dirX, 1, 1);
        }
        //플레이어의 속도에 따라서 애니메니션 속도를 바꾸자 
        this.anim.speed = speedX / 2.0f;
    }
}

씬전환

SceneManager.LoadScene을 사용하면 씬을 전환시킬 수 있다.

//구현할 것: 위로막는거 보완, 2중 점프막고 맵고치기

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

public class ClearDirector : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadScene("GameScene");
        }
    }
}

 

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public GameObject catGo;//cat 게임오브젝트
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (this.catGo.transform.position.y > 0)
        {
            this.transform.position
               = new Vector3(this.transform.position.x, this.catGo.transform.position.y, this.transform.position.z);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CatController : MonoBehaviour
{
    float minX;
    float maxX;
    private Rigidbody2D rBody2D;
    private float jumpForce = 350f;//점프하는힘
    private float moveForce = 30f;//이동하는힘
    private Animator anim;
    private int dirX = 0;

    // Start is called before the first frame update
    void Start()
    {
        this.rBody2D = this.GetComponent<Rigidbody2D>();
        this.anim = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        this.Clamp();
        this.ClampJump();
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //점프한다.
            this.rBody2D.AddForce(Vector2.up * this.jumpForce);
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            dirX = -1;
            
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            dirX = 1;
            
        }

        float speedX = Mathf.Abs(this.rBody2D.velocity.x);

        if (speedX < 2f)
        {
            // this.rBody2D.AddForce(new Vector2(dirX,0) * this.moveForce);
            this.rBody2D.AddForce(this.transform.right * dirX * this.moveForce);
        }
        //방향에 따라 반전
        if(dirX != 0)
        {
            this.transform.localScale = new Vector3(dirX, 1, 1);
        }
        //플레이어의 속도에 따라서 애니메니션 속도를 바꾸자 
        this.anim.speed = speedX / 2.0f;



    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.LogFormat("OnTriggerEnter2D: {0}", collision.name);
        Debug.Log("Game Clear 씬으로 전환");
        SceneManager.LoadScene("ClearScene");
    }

    private void Clamp()
    {
        minX = -2.5f;
        maxX = 2.5f;
        var value = Mathf.Clamp(this.transform.position.x,minX,maxX);
       // Debug.Log(value);   
        if (value == minX || value == maxX)
        {//양끝에 도달하면 멈추기        
            var pos = this.transform.position;
            pos.x = value;
            this.transform.position = pos;
        }      
    }
    private void ClampJump()
    {
       float minY = -4.7f;
       float maxY = 3.5f;
        var value = Mathf.Clamp(this.transform.position.y, minY, maxY);
        // Debug.Log(value);   
        if (value == minY || value == maxY)
        {//양끝에 도달하면 멈추기        
            var pos = this.transform.position;
            pos.y= value;
            this.transform.position = pos;
        }
    }
}
myoskin