- Home /
Problem with reflection against rigidbody
I have an object that moves based on AddRelativeForce in a given direction. The object changes direction slightly and the force is added via FixedUpdate(). Now, I want to make this object change direction when it hits a wall. Put shortly, I want the object to change direction in a way that perfectly mirrors the direction of the collision, like this. The FixedUpdate() function would take care of moving the object in this new direction.
So far, I have this code:
private int count;
public int moveSpeed = 500;
public float maxVelocity = 10.0f;
private float lookSpeed = 15.0f;
// rotation variables
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start ()
{
count = 0;
}
void FixedUpdate ()
{
//alter direction slightly every 5 counts
if (count == 5)
{
rotationX += Random.Range (-0.2f, 0.2f) * lookSpeed;
rotationY += Random.Range (-0.2f, 0.2f) * lookSpeed;
rotationY = Mathf.Clamp (rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis (rotationY, Vector3.left);
count = 0;
}
count++;
//apply force in given direction, with limit
if (rigidbody.velocity.magnitude > maxVelocity)
{
rigidbody.velocity = rigidbody.velocity.normalized * maxVelocity;
}
rigidbody.AddRelativeForce(moveSpeed * Vector3.forward);
}
//reflect off wall
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "wall") {
ContactPoint hit = collision.contacts[0];
Vector3 reflection = Vector3.Reflect (rigidbody.velocity, hit.normal);
rotationX = reflection.x;
rotationY = reflection.y;
}
}
As it stands, however, the object tends to turn in the direction of the object it is colliding with. i.e.: it's running right into it.
Is there something wrong with my code?
Thanks in advance!
I think you will want to set the rigidbody's velocity to the reflected vector that you're calculating, right now it seems you're just changing the rotation of the object.
rigidbody.velocity = Vector3.Reflect(rigidbody.velocity, hit.normal);
That should behave as expected, if you want the rotation to align to it as well as reflect the velocity you can use LookRotation and the normal direction. Depending on your model you might want to reverse the normal direction (-hit.normal).
transform.rotation = Quaternion.LookRotation(hit.normal);
Thanks for the reply. The reason I didn't want to change the velocity in this function is that rotationX and rotationY are global variables that change the direction of the object after which AddRelativeForce takes care of the velocity in FixedUpdate.
I changed my code to:
if (collision.collider.tag == "wall") {
Vector3 oldVelocity = rigidbody.velocity;
ContactPoint hit = collision.contacts[0];
Vector3 reflection = Vector3.Reflect (oldVelocity, hit.normal);
rigidbody.velocity = reflection;
Quaternion rotation = Quaternion.FromToRotation (oldVelocity, reflection);
transform.rotation = rotation * transform.rotation;
rotationX = transform.rotation.x;
rotationY = transform.rotation.y;
}
It seems to handle certain wide angle collisions O$$anonymous$$, but still gets stuck when hitting a wall perpendicularly. Any suggestions?
Hmm... Yeah I was thinking that setting its rotation could possibly shove some parts of the model inside the surface. I'm not exactly sure why it would get stuck though.
Perhaps you can completely replace the reflection with a physic material with a high bounciness setting? If not maybe you can move the object in the normal direction until it's outside the surface? I'm not sure about the best way to do that though, maybe testing bounds intersection...
I'm kinda revisiting the way the fish moves. Is there a way to simply edit rigidbody.velocity directly, ins$$anonymous$$d of having addrelativeforce? I'm trying but it's giving me some weird results.
you might also want to check the "Default Contact Offset" located in Edit->Project Settings->Physics
This solved one of my reflection problems, when my objects velocity was increased. The objects would scatter in unpredictable ways until I reset it from .01 to .06
This creates a larger 'hotspot' for the collision detector, so that your object doesn't touch the object its colliding with.
A higher "Solver Iteration Count" is supposed to give you higher resolution collisions as well, although I couldn't tell much difference, and made things much slower.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141337im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get angle to retarget collision object. 1 Answer
Bounce when hitting wall 2 Answers
Player rotating after wall collision 1 Answer
Questions about rotation. 1 Answer
Unrealistic bounce 2 Answers