반응형
코드 작성
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D playerRb;
private bool isOnMovingPlatform = false;
private Transform currentPlatform;
void Start()
{
playerRb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 플레이어가 움직이는 플랫폼 위에 있는지 확인
if (isOnMovingPlatform)
{
// 플레이어를 플랫폼과 함께 움직이도록 조정
Vector3 platformVelocity = currentPlatform.GetComponent<Rigidbody2D>().velocity;
playerRb.velocity = new Vector2(platformVelocity.x, playerRb.velocity.y);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
// 플레이어가 움직이는 플랫폼과 충돌하는지 확인
if (collision.gameObject.CompareTag("MovingPlatform"))
{
isOnMovingPlatform = true;
currentPlatform = collision.transform;
}
}
void OnCollisionExit2D(Collision2D collision)
{
// 플레이어가 움직이는 플랫폼을 떠났는지 확인
if (collision.gameObject.CompareTag("MovingPlatform"))
{
isOnMovingPlatform = false;
currentPlatform = null;
}
}
}
반응형
'개발 > C#' 카테고리의 다른 글
유니티 C# 자동 전투 적 인공지능 간단 구현하기 (0) | 2024.02.29 |
---|---|
유니티 C# 리듬게임 만들기 Rhythm Game 예시 간단 구현 (0) | 2024.02.29 |
유니티 C# 초보자도 쉽게 따라하는 핵심 튜토리얼 자습서 간단 예시 (0) | 2024.02.29 |
유니티 C# 방치형 클릭커 게임 예시 간단 구현하기 (0) | 2024.02.28 |
유니티 C# 애니메이터 사용법 간단 구현 Animator (0) | 2024.02.26 |
유니티 C# 게임 오브젝트 메세지 전달 간단 구현 SendMessage (0) | 2024.02.26 |
유니티 C# 씬 병합하기 간단 구현 Scene Additive (0) | 2024.02.26 |
유니티 C# 월드 좌표 로컬 좌표 변환 간단 구현 World Position Local Position (0) | 2024.02.26 |
댓글