0

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);
6
  • Maybe you could try without adding currentY, since transform.rotation already uses its current facing direction to rotate, so Quaternion.Euler(0f, (Random.Range(-10.0f, 10.0f)), 0f) would already tell it to turn left or right from its current direction. Commented Jun 23, 2017 at 2:42
  • What exactly is she making? Move forward till what happens? I think it would be best for her to create an account and ask this question directly. Commented Jun 23, 2017 at 2:58
  • I believe you need to remove the + currentY from your equation. Euler is rotates x degrees - you're asking it to rotate x + currentY degrees Commented Jun 23, 2017 at 3:31
  • @Programmer It's perfectly valid for a guardian to post a question on someone's behalf. Also; the question doesn't have anything to do with moving forward until - they're simply asking about rotation. What they're making isn't really relevant here. Commented Jun 23, 2017 at 3:34
  • Yes, it is valid. It's just that I didn't understand the question and I think it would be more helpful for the person that's having the problem to make the post since you can easily use the comment section to ask them question about their question. If you know what they are making, you can easily spot the problem in the code. Commented Jun 23, 2017 at 3:43

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.