유니티 기초

챕터6 고양이 점프[점프막기, 카메라 아래 최소치 정하기 미완]

다모아 2023. 8. 2. 18:15

CatController

using System.Collections;
using System.Collections.Generic;
using UnityEditor.U2D.Animation;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CatController : MonoBehaviour
{
    private Rigidbody2D rBody2D;
    public float jumpForce = 10f; // 점프하는 힘
    private float moveForce = 30f; //이동하는 힘
    private Animator anim;

    // Start is called before the first frame update
    void Start()
    {
        //this.gameObject.GetComponent<Rigidbody2D>();
        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(방향 * 힘);
            this.rBody2D.AddForce(Vector2.up * this.jumpForce, ForceMode2D.Force);
        }

        //-1 0 1 (방향)
        int dirX = 0;

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

        //Debug.Log(this.rBody2D.velocity.x);

        //ForceMode2D.Force : 지속적으로 힘을 가할 때
        //ForceMode2D.Impulse : 충격을 줘서 힘을 가할 때

        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("{0}", collision.name);
        Debug.Log("Game Clear씬으로 전환!");
        SceneManager.LoadScene("ClearScene");
    }
}

 

ClearDirector

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("ClimbCloudScene");
        }
    }
}

 

CameraController

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()
    {
        this.transform.position 
            = new Vector3(this.transform.position.x, this.catGo.transform.position.y, this.transform.position.z);
    }
}

'유니티 기초' 카테고리의 다른 글

챕터7 밤송이 [수정필]  (0) 2023.08.04
pixel_sword  (0) 2023.08.03
챕터5 고양이탈출 - Prefab  (0) 2023.08.02
챕터5 고양이탈출  (0) 2023.08.01
자동차 스와이프 SwipeCar  (0) 2023.08.01