- Home /
Rotating GameObject with transform.Rotate issue!
Hello, I just ran into another issue, I'm trying to rotate a needle to depict a speedometer, I'm getting the following error, see below:
Code:
var currentSpeed : float;
var SpeedNeedle : GameObject;
function Update () {
carsSpeed ();
Speedometer ();
}
function Speedometer () {
var speedFactor : float = currentSpeed;
var rotationAngle : float;
if (currentSpeed >= 0){
rotationAngle = Mathf.Lerp(0,180,speedFactor);
}
else {
rotationAngle = Mathf.Lerp(0,180,-speedFactor);
}
SpeedNeedle = transform.Rotate(0,0,rotationAngle, Space.Self);
}
function carsSpeed () {
//Getting current speed of the tire for engine sounds
currentSpeed = rigidbody.velocity.magnitude * 2.237 * Time.deltaTime;
}
Here is the Error:
Thanks for the help Daniel
PS: I take the time to Mark and rate correct answers.
I don't have time to review the code to figure out if it will work like you want, but for the specific problem listed in the error:
SpeedNeedle.transform.Rotate(0,0,rotationAngle, Space.Self);
As a quick guess, I believe you will really want:
SpeedNeedle.transform.eulerAngles = Vector3(0,0,rotationAngle);
A note not related to the origial question: You might be trying to lerp with a ratio that isn't restricted to inclusive [0,1] range. Ins$$anonymous$$d you should be using a ratio of currentSpeed/maxSpeed to lerp with, so the correct rotation is returned..
Answer by perchik · Jul 31, 2013 at 10:37 PM
Your problem is this: SpeedNeedle = transform.Rotate(0,0,rotationAngle, Space.Self);
SpeedNeedle is of type GameObject. If you look at the documentation for Transform.rotate, you will see that it returns void. The fix is that instead of setting it equal, you should do this:
SpeedNeedle.transform.Rotate(0,0,rotationAngle,Space.Self);
Thank you :D that worked! I forgot about that, of course you cant convert it DUH, haha thank you!
$$anonymous$$ay your $$anonymous$$arma be raised to 1$$anonymous$$, Ah it did! :D
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help to understand code! 1 Answer
Int and Javascript help 2 Answers
Boxcollider 2D Destroyed 2 Answers
Unexpected char : 0x0 2 Answers