- Home /
How to get Steering Wheel Rotation angle?
Hi Everyone! I want to get a steering wheel rotation angle, to aply it on car rotation.
But I'm Using EulerAngles, and it's returns only 360 type angle, without negative angles. I tried to get negative, and it works, but only from 0 to -180, and from 0 to 180.
How can I register , for example, a 720 negative rotation?
Thank you!
public float SteeringWheelAngle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
SteeringWheelAngle = transform.localEulerAngles.y;
SteeringWheelAngle = (SteeringWheelAngle > 180) ? SteeringWheelAngle - 360 : SteeringWheelAngle;
}
Comment
Answer by elenzil · Jun 14, 2017 at 06:26 PM
one way is to track the previous value and handle the case where it wraps around.
private float _syntheticAngle = float.NaN;
private float _prevAngle = float.NaN;
// Update is called once per frame
void Update () {
SteeringWheelAngle = transform.localEulerAngles.y;
SteeringWheelAngle = (SteeringWheelAngle > 180) ? SteeringWheelAngle - 360 : SteeringWheelAngle;
if (float.isNan(_syntheticAngle)) {
_syntheticAngle = SteeringWheelAngle;
_prevAngle = SteeringWheelAngle;
}
float dAngle = SteeringWheelAngle - _prevAngle;
if (dAngle < -180) {
// eg it hopped from -170 to +170
dAngle += 360;
}
else if (dAngle > 180) {
// eg it hopped from +170 to -170
dAngle -= 360;
}
_syntheticAngle += dAngle;
}
Your answer
Follow this Question
Related Questions
physics & rotation question 1 Answer
Can anyone clearly explain what Quaternion.angleaxis is 2 Answers
Smooth rotation to child object 1 Answer
Rotation with fixed position . , 1 Answer
How to make any dice have a given face(int) face up? 1 Answer