- Home /
Find the Vector to divert from colliding?
Hello Devs,
I have a rigidbody which is applied force on it and it tends to move around the wold space. Now at times, it might reach a circular object and I do not want to hit the collider. Lets say that, that circular object (Collider) is a static object and the rigidbody is always on the movement.
Pictorial Representation :
The above image can also be found here.
Main Issue:
Now the problem is I do not want to change the force which is already being applied. I just want to add a force that would divert the rigidbody from colliding. For example, lets say a rigidbody is moving forward, I think that if we apply vector3.right or vector3.left to divert it.
Thank you.
Answer by kaolas · Nov 30, 2012 at 06:44 PM
It sounds like you want to get the cross product of the current vector. The Vector3.Cross() function will get the perpendicular 3rd vector to any given two vectors you provide it. So for example:
var diversionVector = Vector3.Cross(transform.forward, transform.up);
The diversionVector is a perpendicular vector that you could use to apply additional force to divert your object. You can negate the diversionVector to make it go the opposite direction if necessary (i.e. left instead of right).
kaolas -> Thank you. When you say transform.forward does it mean local forward or global forward? I also assume that the transform is the rigidbody.
Thank you.
"transform.forward" would be the current forward vector for your object based on its current rotation, so that would translate to the "local" forward. It isn't necessarily the same as Vector3.forward (though it could be). This is all, of course, assu$$anonymous$$g your object rotates to face the direction it's traveling. If it isn't, you could always use the direction of the force being applied to the object which is pushing it toward the collision that you want to avoid. Using that vector as your "forward", the cross product would provide you with a perpendicular vector to the force ins$$anonymous$$d of the direction the object is facing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to compute the change in vector when it's referential vector changes? 0 Answers
Performance Question on Kinematic Rigidbodies 1 Answer
Force iOS first person to move slightly when player is not moving it? 1 Answer
Apply vertical Force then return it to Zero on release Key 1 Answer