TrajectoryObject bounce off vertical walls

Zaddo

Active member
I found that TrajectoryObjects were sometimes sticking to walls and over hangs. This mod will adjust the reflection velocity based on the vertical angle of the surface. This stops thrown items from attaching to surfaces they shouldn't. This code change is to the Move method of the TrajectoryObject class.


From:
C#:
// The bounce strenght is dependent on the physic material.
var dynamicFrictionValue = m_Collider != null ? Mathf.Clamp01(1 - MathUtility.FrictionValue(m_Collider.material, m_RaycastHit.collider.material, true)) : 0f;
// Update the velocity to the reflection direction.
m_Velocity = dynamicFrictionValue * m_ReflectMultiplier * Vector3.Reflect(velocity, m_RaycastHit.normal);

To:
C#:
// The bounce strenght is dependent on the physic material.
var dynamicFrictionValue = m_Collider != null ? Mathf.Clamp01(1 - MathUtility.FrictionValue(m_Collider.material, m_RaycastHit.collider.material, true)) : 1f;

// adjust bounce for vertical surface
float maxBounce = Mathf.Max(m_ReflectMultiplier, 1f);
var verticalAngle = Vector3.Angle(m_RaycastHit.normal, Vector3.up);
var reflectionMultiplier = dynamicFrictionValue * m_ReflectMultiplier;
var bounceMultiplier = Mathf.Clamp(reflectionMultiplier + (maxBounce - reflectionMultiplier) * (verticalAngle / 180f), 0f, maxBounce);

// Update the velocity to the reflection direction.
m_Velocity = bounceMultiplier * Vector3.Reflect(velocity, m_RaycastHit.normal);
 
Last edited:
I was having this same problem with them sticking in walls, but I ended up removing them from weapons altogether for better performance. Now that I think about it, maybe when they stuck in the walls the continuous collisions were causing the physics performance issue I was having.
 
Top