How do I return a float value between 1 and -1 based on a camera's rotation?
I have a have a ball with a rigidbody attached and I am adding torque to the ball based on the angle of a third person camera witch rotates around the ball. I am trying to change the direction of the ball based on the position of the camera in its rotation around the ball. I am using Vector3.right
for forward and reverse torque and Vector3.back
for right and left. I have the forward/reverse value, rightVal
working but I have been working on the left/right value, backVal
for three days and am at a loss. Here is how I am trying to accomplish this.
void CalcRightVal()
{
float degrees = Camera.main.transform.localEulerAngles.y;
if (degrees > 0 && degrees < 180.0f)
{
rightVal = (-degrees / 360) * 4 + 1;
}
if (degrees > 180.0f && degrees < 360.0f) //
{
rightVal = (-degrees / 360) * 4 + 1;
if (rightVal < -1.0f)
{
rightVal = (degrees / 360) * 4 - 3;
}
}
}
void ApplyTorque()
{
Vector3 dir = new Vector3(rightVal, 0, backVal);
ball.AddTorque(dir * ballAcceleration);
}
In ApplyTorque()
I need the backVal
to go from 0 to -1 i.e. Vector3.back
when degrees == 90
. Then backVal
should increase in value to 0 when degrees == 180
then up to 1 when degrees == 270
then back to 0 when degrees == 360
. This will apply torque to the right when the camera is at 90 degrees and left when the camera is at 270 degrees. The Idea is that the user will add input to forward i.e. KeyCode.W
to add acceleration and reverse or KeyCode.S
for breaks. The camera will be responsible for changing the direction of the force. I am not terrible at math but this one has me beat.
Here is a graphic to help out. Thank You.
Answer by Alanisaac · Feb 20, 2018 at 01:47 AM
I could be misinterpreting, but the way you describe it, it sounds like the end goal is to get a 2D normalized vector in the direction from the camera to the ball, and then apply ballAcceleration
amount of torque in that direction. This doesn't necessarily answer your original question, but I'm guessing there's a better way to avoid doing all that math yourself. Would the following work?
void ApplyTorque()
{
var cameraPosition = Camera.main.gameObject.transform.position;
var direction = ball.position - cameraPosition;
var planarDirection = new Vector3(direction.x, 0, direction.z).normalized;
ball.AddTorque(planarDirection * ballAcceleration);
}
EDIT: based on the comment below, it sounds like you want a vector 90 degrees off from the direction the camera is facing. You can do that by getting a normal/perpendicular vector to the camera's direction. In that link, you'll notice you need two vectors representing your plane to get a normal. In your case we can use the vector from the camera to the ball as the first vector, and "up" as the second:
void ApplyTorque()
{
var cameraPosition = Camera.main.gameObject.transform.position;
var direction = ball.position - cameraPosition;
var planarDirection = new Vector3(direction.x, 0, direction.z);
var perpendicularDirection = Vector3.Cross(planarDirection, Vector3.up).normalized;
ball.AddTorque(planarDirection * ballAcceleration);
}
I'm still not clear on which direction the torque vector is rotated from the camera (clockwise or counterclockwise) -- BUT if you find this script works almost perfectly except backwards, just reverse the perpendicularDirection
by making it negative.
As I look over my question I notice that I failed to lay out a key parameter. While rightVal
is at 1 or -1, backVal
needs to be at 0 and vice versa. That way when the camera is directly behind the ball or 0 degrees the torque applied will be Vector3.right
or (1, 0, 0)
and when the camera is at 90 degrees or on the left of the ball, torque applied will be Vector3.back
or (0, 0, -1)
. This will apply torque to make the ball turn right. Basically wherever the camera is the ball rolls forward from the point of view of the user. Thank you for the help, and all help is always appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612153006im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque 0 Answers
Inspactor Roation to Vector3 0 Answers
Why isn't my steering wheel working? 0 Answers
How can I deal with eulers 1 Answer
Rotate object towards Vector 0 Answers