Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

moonee-e3 님의 블로그

204-06-14 | 움직이는 땅 본문

개발 일지 [3D | Dream Forest]

204-06-14 | 움직이는 땅

moonee-e3 2024. 6. 23. 01:01
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingGround : MonoBehaviour
{
    public Transform startPos;
    public Transform endPos;
    private Transform desPos;
    public float Groundspeed;

    void Start()
    {
        transform.position = startPos.position;
        desPos = endPos;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.position = Vector3.MoveTowards(transform.position, desPos.position, Time.deltaTime * Groundspeed);

        if (Vector3.Distance(transform.position, desPos.position) <= 0.05f)
        {
            if (desPos == endPos) desPos = startPos;
            else desPos = endPos;
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.transform.CompareTag("Player") || (collision.transform.CompareTag("Box")))
        {
            collision.transform.SetParent(transform);
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.transform.CompareTag("Player") || (collision.transform.CompareTag("Box")))
        {
            collision.transform.SetParent(null);
        }
    }
}