왼쪽 기즈모(startPos)는 발판의 시작점, 오른쪽 기즈모(endPos)는 발판의 도착지점입니다.
플레이어가 발판 위에 있으면 발판은 오른쪽으로 이동해 목적지에 도달할 것이고 내리면 발판은 스스로 초기 위치로 이동해야 합니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoveringPlane : MonoBehaviour
{
public bool isRide;
private float speed = 3f;
public Transform startPos;
public Transform endPos;
public GameObject player;
void Start()
{
player = GameObject.FindWithTag("Player");
isRide = false;
}
void Update()
{
if (isRide && transform.position != endPos.position)
{
transform.position = Vector3.MoveTowards(transform.position, endPos.position, Time.deltaTime * speed);
}
if (!isRide)
{
transform.position = Vector3.MoveTowards(transform.position, startPos.position, Time.deltaTime * speed);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
isRide = true;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
isRide = false;
}
}
}
하지만 문제가 생겼습니다. 발판 위에 있는 플레이어의 위치는 이동에 영향을 받지 않고 있었습니다.
이 문제는 간단히 해결할 수 있었는데요,
업데이트 함수에서 '발판을 탑승했을 때 플레이어의 위치값'을 추가하면 정상적으로 작동하게 됩니다.
결과: