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

public class BulletController : MonoBehaviour
{
    [SerializeField] private float force = 1000f;
    [Range(0, 1)][SerializeField] private float reflectionBias = 0.5f; // 반사각도 조정
    private Rigidbody rBody;

    void Start()
    {
        rBody = this.GetComponent<Rigidbody>();
        rBody.AddRelativeForce(Vector3.forward * this.force);
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Wall"))
        {
            Vector3 reflectDirection = Vector3.Reflect(rBody.velocity.normalized, collision.contacts[0].normal);
            reflectDirection = Vector3.Lerp(reflectDirection, collision.contacts[0].normal, reflectionBias);
            rBody.velocity = reflectDirection * rBody.velocity.magnitude;

            // 반사된 방향으로 총알 회전
            transform.rotation = Quaternion.LookRotation(rBody.velocity);

            // 총알의 회전 속도를 0으로 설정
            rBody.angularVelocity = Vector3.zero;
        }
    }
}

반응형

+ Recent posts