On iOS, I have a wrecked car with a Rigidoby that should be pushed towards a specified target in the space (that target is the position where you touch the screen). The force must be applied only on the XZ plane, so no need to calculate Y height. Actually, I can find correctly the touch point. Consider that my car doesn't need to stop moving on the target, but just to slide towards it until it stops (before or after reaching the target, it doesn't matter).
I wanted to use `rigidbody.AddForce` for this task. Since the direction of must be expressed with a `Vector3`, i can't specify the precise target position but I have to calculate the angle between the car and the touch point. And here comes the problem: looks like I can't find this angle, no matter how I try.
I tried using this:
var myAngle = (Vector2.Angle(Vector2(carPos.x,0,carPos.z),Vector2(touchPos.x,0,touchPos.z)))*Mathf.Deg2Rad;
rigidbody.AddForce(Mathf.Cos(myAngle),0,Mathf.Sin(myAngle));
This is giving really wrong results. I tried to get `myAngle` with other three different math functions (wich I found on the web, one was also suggested by my Math teacher), but nothing worked, the angle is always extremely wrong, small, sometimes I get NaN errors.
So, this is the question: how can I calculate the angle between point A and B, relative to the X axis, on an XZ plane? Please consider I can't rotate my car towards that point, it does need to mantain its actual rotation (leaving it to rotate again if it hits something).
Many thanks to everyone who can help. :)
It's working perfectly, thank you so much! :D
– DanjelRicci