So I’m trying to replicate the physics concept of a binding force (not sure how it’s called in English, sorry). Basically, when the character collides with something, I want to neutralize the component of the force that is pointing towards that something. I’m pretty sure that unity’s physics system does that on it’s own, but since I’m using
RigidBody.velocity = moveSpeed;
instead of AddForce, I have to do it myself.
This is what I came up with:
void OnCollisionStay(Collision col)
{
Vector3 colNormal = col.GetContact(0).normal;
Debug.DrawRay(transform.position, colNormal * 2, Color.green);
float Dot = Vector3.Dot(controller.moveSpeed.normalized, colNormal.normalized);
Debug.Log("Normal is " + colNormal * controller.moveSpeed.magnitude * Dot);
controller.moveSpeed += colNormal * controller.moveSpeed.magnitude * Dot;
}
The ray is drawn exactly how it should be, so the normal is correct, and the Debug.Log also writes a Vector3 that is not NaN. But when I do the final addition the moveSpeed vector gets turned into NaN. Can anyone help?