- Home /
Find direction from current direction rotated by angle
Hello,
I have 2 balls in a Unity 3D project. One ball is moving and the other is not. I am trying to find a direction based off the current direction that my ball is moving. I would like to find the angle between my current direction and the direction between the two balls, then rotate my current direction by that angle in the opposite direction to get another direction. The balls rotate on every axis when they move so I can not use any Transform.rotate functions. If anyone knows how to do this that would be greatly appreciated. I've attached a picture describing the problem.
Image:
Answer by Eno-Khaon · Jul 06, 2021 at 03:39 AM
Based on your image, there are multiple solutions to your problem:
This example is the pure Vector3 approach, using Vector3.Reflect(). Calculate the reflection of the first ball's heading against the normal of a plane (symbolic, not literal) formed passing through both balls:
// The heading of ball1 (e.g. its current velocity
Vector3 ball1Heading = ball1Rigidbody.velocity;
// The vector from ball1 to ball2
Vector3 ball1To2Dir = (ball2Transform.position - ball1Transform.position);
// A perpendicular vector to the previous two,
// unable to be found if heading and from-to are parallel
Vector3 perpendicular = Vector3.Cross(ball1Heading, ball1To2Dir);
// The reflection normal, obtained utilizing the new perpendicular vector
Vector3 reflectionNormal = Vector3.Cross(ball1To2Dir, perpendicular).normalized;
// The reflection of ball1's heading against the reflectionNormal
Vector3 reflectedHeading = Vector3.Reflect(ball1Heading, reflectionNormal);
Your answer
Follow this Question
Related Questions
How to detect if player is facing towards enemy's back 2 Answers
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer
Calculate a new position instead of using Vector3.back 1 Answer
Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers
How Do I accurately rotate a player 180 degrees about the Z axis? 1 Answer