using System.Collections;
using UnityEngine;

public class TestPlayer : MonoBehaviour
{
    [SerializeField]
    private Transform eye;
    public float moveSpeed = 5.0f;

    private void Start()
    {
        MoveForward();
    }
    public void MoveForward()
    {
        Ray ray = new Ray(this.eye.position, this.transform.forward);
        RaycastHit hitInfo;


        if (Physics.Raycast(ray, out hitInfo))
        {
            Debug.DrawRay(ray.origin, ray.direction * hitInfo.distance, Color.blue, 5f);


            StartCoroutine(CoMoveForward(hitInfo.distance, hitInfo.normal));
        }
        else
        {
            Debug.DrawRay(ray.origin, ray.direction * 100f, Color.blue, 5f);
        }
    }

    private IEnumerator CoMoveForward(float distance, Vector3 wallNormal)
    {
        float moveAmount = 0;
        while (moveAmount < distance)
        {
            float step = moveSpeed * Time.deltaTime;
            moveAmount += step;
            this.transform.Translate(Vector3.forward * step);


            if (moveAmount > distance)
            {
                float excess = moveAmount - distance;
                this.transform.Translate(Vector3.back * excess);
            }

            yield return null;
        }


        Vector3 reflectDirection = Vector3.Reflect(this.transform.forward, wallNormal);
        this.transform.rotation = Quaternion.LookRotation(reflectDirection);
    }
}

반응형

+ Recent posts