Reputation: 58
I want to emulate a snake slithering randomly. This is in Update()
however, the rotation isn't exactly what I want.
My game object's y rotation starts at 270 degrees. At first it seems to work but it always seems to end up pointing approx. 360 degrees.
float currentY = transform.rotation.y;
// the initial y rotation starts at 270
// seems like it rotates y to be between 350 and 10 degrees only.
// This is not what I want. I want it to randomly turn a little to the left or right,
// and then move forward
transform.rotation = Quaternion.Lerp(
transform.rotation,
Quaternion.Euler(0f, (Random.Range(-10.0f, 10.0f) + currentY), 0f),
(float)(Time.time * .01)
);
rbody.AddForce(transform.forward * speed * 2.5f);
Upvotes: 0
Views: 209
Reputation: 58
Well, she got it yesterday. I think it was that .y was not an angle, as someone said, but I think she realized that herself. Thanks for the help!
Upvotes: 0
Reputation: 27357
You're nearly there. The following should work:
float currentY = transform.rotation.y;
// Randomly rotate +- 10 degrees on the y axis
var rotateBy = Random.Range(-10.0f, 10.0f);
transform.rotation = Quaternion.Lerp(
transform.rotation,
Quaternion.Euler(0f, rotateBy, 0f),
(float)(Time.time * .01)
);
rbody.AddForce(transform.forward * speed * 2.5f);
Previously, you were asking it to rotate the y
axis by y +- 10
. What you're really wanting to do is rotate the y
axis by +- 10
, without adding the current y
value.
That is; you were assuming it would rotate to the specified angle, however it's going to rotate by the specified angle.
Upvotes: 3