I’m having a problem where my sphere sometimes just goes through an object, bounces back as if the velocity was inverted, and sometimes also reflects it, but mirrored?
Not sure how I should explain, so here’s a -video-.
My code for the reflecting and stuff is here :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
private Rigidbody m_bulletRigidbody;
private RaycastHit m_rayHit;
private bool m_raycastDidHit;
private void Start()
{
m_bulletRigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
Debug.DrawRay(transform.position, m_bulletRigidbody.velocity.normalized, Color.red);
m_raycastDidHit = Physics.Raycast(transform.position, m_bulletRigidbody.velocity.normalized, out m_rayHit);
}
private void OnTriggerEnter(Collider other)
{
if (m_raycastDidHit)
{
if (other.gameObject.tag != "Player" && other.gameObject.tag != "LocalPlayer" && other.gameObject.tag != "Bullet")
{
if (m_rayHit.transform.gameObject.tag != "Bullet" && m_rayHit.transform.gameObject.tag != "LocalPlayer" && m_rayHit.transform.gameObject.tag != "Player")
{
Vector3 reflected = Vector3.Reflect(m_bulletRigidbody.velocity.normalized, m_rayHit.normal);
reflected.x *= m_bulletRigidbody.velocity.x;
reflected.y *= m_bulletRigidbody.velocity.y;
reflected.z *= m_bulletRigidbody.velocity.z;
m_bulletRigidbody.velocity = reflected;
}
}
}
}
}
Anyone who can explain why this is happening? :o