using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CatController : MonoBehaviour
{
private Rigidbody2D rBody2D;
[SerializeField] private float jumpForce = 10f; // 점프하는 힘
[SerializeField] private float moveForce = 30f; // 이동하는 힘
[SerializeField] private ClimbCloudGameDirector gameDirector;
private Animator anim;
private State currentState = State.Idle;
private bool isJumping = false;
private enum State
{
Idle,
Walk,
Jump
}
void Start()
{
this.rBody2D = this.GetComponent<Rigidbody2D>();
this.anim = this.GetComponent<Animator>();
}
void Update()
{
HandleMovement();
AdjustAnimationSpeed();
}
void AdjustAnimationSpeed()
{
// 캐릭터의 현재 속도
float speed = Mathf.Abs(rBody2D.velocity.x);
// 애니메이션 속도를 조절하기 위한 기준값 설정
float animationSpeed = speed / 2.0f; //
// 애니메이션 속도를 설정합니다. 최소 속도는 1로 유지하여 애니메이션이 멈추지 않도록 함
anim.speed = Mathf.Max(1, animationSpeed);
}
void HandleMovement()
{
if (Input.GetKey(KeyCode.Space) && !isJumping && Mathf.Abs(this.rBody2D.velocity.y) <= 0f )
{
this.rBody2D.AddForce(Vector2.up * this.jumpForce, ForceMode2D.Impulse);
isJumping = true;
SetAnimationState(State.Jump);
}
int dirX = 0;
if (Input.GetKey(KeyCode.LeftArrow))
dirX = -1;
if (Input.GetKey(KeyCode.RightArrow))
dirX = 1;
if (dirX != 0 && !isJumping) // Walk 상태 설정은 점프 중이 아닐 때만
{
SetAnimationState(State.Walk);
}
else if (!isJumping) // 점프 중이 아닐 때 Idle 상태로 설정
{
SetAnimationState(State.Idle);
}
this.transform.localScale = dirX != 0 ? new Vector3(dirX, 1, 1) : transform.localScale;
float speedX = Mathf.Abs(this.rBody2D.velocity.x);
if (speedX < 2f)
{
this.rBody2D.AddForce(this.transform.right * dirX * this.moveForce);
}
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -2.5f, 2.5f), transform.position.y, transform.position.z);
this.gameDirector.UpdateVelocityText(this.rBody2D.velocity);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("ground"))
{
isJumping = false;
if (currentState == State.Jump) // 착지 시 Idle 상태로 변경
{
SetAnimationState(State.Idle);
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("flag"))
{
SceneManager.LoadScene(1);
}
Debug.LogFormat("OnTriggerEnter2D: {0}",collision);
}
private void SetAnimationState(State newState)
{
if (currentState != newState)
{
currentState = newState;
anim.SetInteger("state", AnimationStateToInt(currentState));
}
}
private int AnimationStateToInt(State state)
{
switch (state)
{
case State.Idle:
return 0;
case State.Walk:
return 1;
case State.Jump:
return 2;
default:
return 0; // 기본값으로 Idle 상태 반환
}
}
}
2. CatController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor.SearchService;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ReturnBtn : MonoBehaviour
{
[SerializeField] private Button returnBtn;
void Start()
{
returnBtn.onClick.AddListener(() =>
{
SceneManager.LoadScene(0);
});
}
void Update()
{
}
}
3. ReturnBtn.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
[SerializeField] GameObject catGo;
private Transform cam;
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);
}
}
}
4. CameraController.cs
반응형