유니티 심화

Reflect

다모아 2023. 8. 21. 18:13

TestBulletController

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestBulletController : MonoBehaviour
{
    private Vector3 rotVec;
    private Quaternion rot;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = new Ray(this.transform.position, this.transform.forward);
        RaycastHit hit;
        DrawArrow.ForDebug(ray.origin, ray.direction * 100f, 2f, Color.green, ArrowType.Solid);
        if(Physics.Raycast(ray, out hit, 100f))
        {
            this.rotVec = Vector3.Reflect(-this.transform.forward, hit.normal);
            Debug.LogFormat("ray.direction: {0}, hit.normal: {1}", ray.direction, hit.point);
            DrawArrow.ForDebug(hit.collider.transform.position, ray.direction, 5f, Color.yellow, ArrowType.Solid);
        }

        this.transform.Translate(Vector3.forward * 3f * Time.deltaTime);
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Wall")
        {
            this.transform.rotation = Quaternion.LookRotation(-rotVec);
        }
    }
}