pudding81 2024. 1. 31. 18:58

 
 

 
 
고양이 점프하기
Input.GetKeyDown(KeyCode.Space)
AddForce(this.transform.up * this.jumpforce)
 
점프는 한번만  bool isjump==false
리셋하기 ->  OnCollisionEnter2D(Collision2D col) 
 
좌우로 움직이기
int dirX =0; //방향 멈춤 0 오른쪽 1 왼쪽 -1 
Input.GetKey(KeyCode.RightArrow)
 
좌우 방향에 따라 고양이 방향 같이 바뀌기
this.transform.localScale = new Vector3(dirX, 1, 1);
 
이동속도를 제한하기
Mathf.Abs 절대값
 
화면밖으로 못나가게 하기
Mathf.Clamp(this.transform.position.x, -2.4f, 2.5f)
 
깃발과 충돌할 경우 
 OnTriggerEnter2D(Collider2D collision) 
 

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

public class CatController : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rbody;
    [SerializeField] private float jumpforce = 680f;
    [SerializeField] private float moveforce = 0.003f;
    [SerializeField] private CloudGamedirector gameDirector;
    private Animator anim;
    private bool isjump= false;


    private void Start()
    {
        anim =GetComponent<Animator>();
               

        //this.gameDirector = GameObject.Find("GameObject").GetComponent<CloudGameDirector>();
        //this.gameDirector = GameObject.FindAnyObjectByType<CloudGameDirector>();
    }
    void Update()
    {
        
        //점프
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (isjump==false)
            {
                isjump = true;
                this.rbody.AddForce(this.transform.up * this.jumpforce);//회전방향대로 업
                
            }

                   
           

            //this.rbody.AddForce(Vector3.up * this.force); //직선위로만 업
            //위는 캐릭터의 y축으로 날아가고 아래는 세계의 y축방향으로 날아간다.
        }

       
        //키를 누르고 있는 동안에 좌우이동
        int dirX =0; // 방향 멈춤 0 오른쪽 1 왼쪽 -1 
        


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

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

        

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


        // Debug.Log(dir); //방향

        //속도를 제한하자 


        if (Mathf.Abs(this.rbody.velocity.x) < 3)
        {
            this.rbody.AddForce(this.transform.right * dirX * moveforce);
        }
        
        
        // 플레이어 이동속도에 따라 애니메이션 속도를 조절하자.
        // this.anim.speed = (Mathf.Abs(this.rbody.velocity.x) / 2f);
       

        this.gameDirector.UpdateVelocityText(this.rbody.velocity);

        //화면 밖으로 못나가게 하기
        float clamf = Mathf.Clamp(this.transform.position.x, -2.4f, 2.5f);
        Vector3 pos = this.transform.position;
        pos.x = clamf;
        this.transform.position = pos;


    }


    void OnCollisionEnter2D(Collision2D col) 
    { 
         isjump = false;  
    }

    //충돌 판정 트리거모드일 경우 
    private void OnTriggerEnter2D(Collider2D collision) //최초충돌할때 한번만 
    {
        //장면전환
        Debug.LogFormat("충돌");
        SceneManager.LoadScene("CloudClear");



    }
   
}

 
 
플레이어의 속도 텍스트 

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


public class CloudGamedirector : MonoBehaviour
{
    [SerializeField] private Text velocityText;


    public void UpdateVelocityText(Vector2 velocity)
    {
        //플레이어의 속도
        float velocityX = Mathf.Abs(velocity.x);
        this.velocityText.text = velocityX.ToString();
      
    }

    
}

 
 
카메라의 이동 
transform.position

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

public class CameraController : MonoBehaviour
{
    GameObject player;
   


    // Start is called before the first frame update
    void Start()
    {
        this.player = GameObject.Find("cat");
       
    }

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

        //고양이 y좌표 이동에 따라 카메라도 y 좌표 변동

        if (this.player.transform.position.y> -6f)
        {
            Vector3 playerGo = this.player.transform.position;
             transform.position
                = new Vector3(transform.position.x, playerGo.y, transform.position.z);

            

        }




    }
}

 
 
씬 전환 
SceneManager.LoadScene("")

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

public class Restart : MonoBehaviour
{
    
   void Update()

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

    }

}