본문 바로가기
Unity/수업내용

[Unity 2D / 유니티 교과서] 6장 예제 ClimbCloud

by 왹져박사 2023. 2. 1.
728x90

Rigidbody : 중력, 마찰 힘 계산 (강체)

 -물리 영향을 무시 : Body Type - Kinematic (기본 Dynamic)

 -회전을 방지 : Constraints - Freeze Rotation z활성

 

Collider : 충돌 판정

OnTriggerEnter2D(Colider2D) : 

 

SceneManager

 

자주 사용하게 될 Find method

 -복수형에 주의! 단수형은 처음 찾은 대상을 반환, 복수형은 배열을 반환

  • FindGameObjectsWithTag(태그이름)
  • FindObjectsOfType(타입이름)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Cat : MonoBehaviour
{
    Rigidbody2D rbody2D;
    Animator animator;
    public float jumpForce = 680f;
    public float walkForce = 30f;
    public float maxWalkSpeed = 2.0f;
    // Start is called before the first frame update
    void Start()
    {
        this.rbody2D = this.GetComponent<Rigidbody2D>();
        this.animator = this.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)&&this.rbody2D.velocity.y==0)
        {
            // this.transform.up : (0, 1, 0) : 길이가 1인 방향 벡터
            //길이가 1인 방향벡터를 노멀 벡터
            this.rbody2D.AddForce(this.transform.up * jumpForce);
            //world좌표로 up 하고싶다면 Vector3.up
        }

        //좌우 이동
        int dir = 0;
        if (Input.GetKey(KeyCode.RightArrow)) dir = 1;
        if (Input.GetKey(KeyCode.LeftArrow)) dir = -1;

        //움직이고 있는 강체의 속도
        float speedX = Mathf.Abs(this.rbody2D.velocity.x);
        if(speedX < this.maxWalkSpeed)
        {
            //(1,0,0)
            this.rbody2D.AddForce(this.transform.right * dir * this.walkForce);
        }

        if (dir != 0)
        {
            this.transform.localScale = new Vector3(dir, 1, 1);
        }

        //플레이어 속도에 맞춰 애니메이션 속도 조절
        this.animator.speed = speedX / 2.0f;

        if (this.transform.position.y < -10)
        {
            SceneManager.LoadScene("GameScene");
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("골");
        SceneManager.LoadScene("ClearScene");
    }
}
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 UnityEngine;

public class CameraController : MonoBehaviour
{
    private GameObject catGo;
    // Start is called before the first frame update
    void Start()
    {
        this.catGo = GameObject.Find("Cat");
    }

    // Update is called once per frame
    void Update()
    {
        float catposY = this.catGo.transform.position.y;

        //카메라 위치 설장
        var pos = this.transform.position;
        pos.y = catposY;
        this.transform.position = pos;

    }
}

 

728x90