Skip to main content
edited body
Source Link
DMGregory
  • 136.4k
  • 22
  • 248
  • 374

If you just want to do something with the component of the velocity parallel to the normal, that's just a vector projection, and you don't need to do a full transformation into a local coordinate system with that ambiguous degree of freedom.

Assuming your normal has unit length, that's:

// Measure incoming speed along normal direction.
float normalSpeed = Vector3.Dot(rb.velocity, normal);

// Cancel out the velocity in that direction.
rb.AddForce(-normalSpeed * normal, ForceMode.VelocityChange);

This is also available as a built-in function in Unity:

rb.AddForce(
    -Vector3.Project(rb.velocity, normal), 
    ForceMode.VelocityChange
);

This is why it's important to always frame questions in terms of the ultimate goal you want to achieve, not the stepping stone you thought of to get there. If you don't understand the problem well enough yet to fully solve it, then you might not be picking the right stepping stone, leading to something called the XY Problemthe XY Problem. Here, asking to transform to local coordinates was a red herring, that made the solution more complicated than it needed to be.

If you just want to do something with the component of the velocity parallel to the normal, that's just a vector projection, and you don't need to do a full transformation into a local coordinate system with that ambiguous degree of freedom.

Assuming your normal has unit length, that's:

// Measure incoming speed along normal direction.
float normalSpeed = Vector3.Dot(rb.velocity, normal);

// Cancel out the velocity in that direction.
rb.AddForce(-normalSpeed * normal, ForceMode.VelocityChange);

This is also available as a built-in function in Unity:

rb.AddForce(
    -Vector3.Project(rb.velocity, normal), 
    ForceMode.VelocityChange
);

This is why it's important to always frame questions in terms of the ultimate goal you want to achieve, not the stepping stone you thought of to get there. If you don't understand the problem well enough yet to fully solve it, then you might not be picking the right stepping stone, leading to something called the XY Problem. Here, asking to transform to local coordinates was a red herring, that made the solution more complicated than it needed to be.

If you just want to do something with the component of the velocity parallel to the normal, that's just a vector projection, and you don't need to do a full transformation into a local coordinate system with that ambiguous degree of freedom.

Assuming your normal has unit length, that's:

// Measure incoming speed along normal direction.
float normalSpeed = Vector3.Dot(rb.velocity, normal);

// Cancel out the velocity in that direction.
rb.AddForce(-normalSpeed * normal, ForceMode.VelocityChange);

This is also available as a built-in function in Unity:

rb.AddForce(
    -Vector3.Project(rb.velocity, normal), 
    ForceMode.VelocityChange
);

This is why it's important to always frame questions in terms of the ultimate goal you want to achieve, not the stepping stone you thought of to get there. If you don't understand the problem well enough yet to fully solve it, then you might not be picking the right stepping stone, leading to something called the XY Problem. Here, asking to transform to local coordinates was a red herring, that made the solution more complicated than it needed to be.

Source Link
DMGregory
  • 136.4k
  • 22
  • 248
  • 374

If you just want to do something with the component of the velocity parallel to the normal, that's just a vector projection, and you don't need to do a full transformation into a local coordinate system with that ambiguous degree of freedom.

Assuming your normal has unit length, that's:

// Measure incoming speed along normal direction.
float normalSpeed = Vector3.Dot(rb.velocity, normal);

// Cancel out the velocity in that direction.
rb.AddForce(-normalSpeed * normal, ForceMode.VelocityChange);

This is also available as a built-in function in Unity:

rb.AddForce(
    -Vector3.Project(rb.velocity, normal), 
    ForceMode.VelocityChange
);

This is why it's important to always frame questions in terms of the ultimate goal you want to achieve, not the stepping stone you thought of to get there. If you don't understand the problem well enough yet to fully solve it, then you might not be picking the right stepping stone, leading to something called the XY Problem. Here, asking to transform to local coordinates was a red herring, that made the solution more complicated than it needed to be.