-1

I have a platform that must rotate simultaneously in x, y & z directions in a random order, the maximum degree measure of rotation of vectors x & z is 10 degrees, and rotation in the "y" direction is 360 degrees.

Here is the code:

using UnityEngine;

public class PlaneRotating : MonoBehaviour
{
    void Start() {
     
 }
    void Update()
    {
        Vector3 euler = transform.eulerAngles;
     euler.z = Random.Range(-10f, 10f);
     euler.y = Random.Range(0f, 360f);
     euler.x = Random.Range(-10f, 10f);
     transform.eulerAngles = euler;
    }
}

Everything works perfectly, except for a certain rotation speed, since I don't know how to set it.

Can you tell me how to set the speed?

  • Use a coroutine or invokerepeating and RotateTowards – TEEBQNE Jul 16 '21 at 16:51
  • Could you explain your goal a little more? Your current code is executed every frame and will generate an extremely crazy jittery rotation ... what exactly are you actually trying to achieve? – derHugo Jul 16 '21 at 17:05
  • There will be RigidBodies on the platform that can fall, and my character must catch them and prevent them from falling. The platform should rotate slowly, for example - 2 degrees per second, and it should rotate like ... how to explain, this is the same thing as rotating a notebook in different directions, smoothly and slowly. – Arina Aznauryan Jul 16 '21 at 17:18

1 Answers1

0

You could lerp between positions, its not the best way to control the speed because large rotations will be faster and smaller rotations will be slower but you can have a stable rotations, time wise.

using UnityEngine;

public class PlaneRotating : MonoBehaviour
{
    public Transform transform;
    Vector3 lastPos
    Vector3 newPos
    float t;

     void Start()
     {
         lastPos = transform.eulerAngles;
         NewAngle();
     }

     void Update()
     {
         transform.eulerAngles = Vector3.Lerp(lastPos, newPos, t);
         t+=0.01f;
         if(t>1)
             NewAngle();
     }

    void NewAngle()
    {
        lastPos = newPos;
        newPos = new Vector3(
                     Random.Range(-10f, 10f),
                     Random.Range(0f, 360f),
                     Random.Range(-10f, 10f));
        t = 0;
    }
}

you can then play with Time.deltaTime to make it consistent this is only an example.

If you want to control speed

you can just add two vectors and you do the randomization periodically every x seconds or x frames. you change the direction. there are plenty of good videos on youtube but you need to be more clear if you want to have a good answer. my first answer doewnt let you control the speed with random numbers, only makes smooth transitions and you can choose how fast they change. for constant speeds you have to do something like the bellow

 void Update()
 {
     transform.eulerAngles += new Vector3(0,1,0);
 }

let me know if I can be of any further help.

Barreto
  • 374
  • 2
  • 14
  • Thanks, thanks to your code, the platform rotates absolutely unpredictably, and even with stops, that's exactly what I needed! But only rotations in the x & z direction - exceed their degree measure - 10 degrees. And by the way, can you tell me why you added the "if (t>1)" condition? What is it for? – Arina Aznauryan Jul 16 '21 at 17:26
  • glad it was usefull, the "if(t>1)" is to make sure "t" doesnt go beyond 1, since lerp needs a value between 0 and 1. and the "t+=0.01f" controls how fast it changes from one position to the next – Barreto Jul 16 '21 at 17:35
  • 1
    You could rather use `RotateTowards` in order to control the 2° per second as requested by OP ;) – derHugo Jul 16 '21 at 17:39
  • aren't you restricting the y axis in the rigid body? (constraints, freeze rotation) – Barreto Jul 16 '21 at 17:39
  • @derHugo i didnt know about that actualy:) but sounds good [upon looking it up](https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html) – Barreto Jul 16 '21 at 17:42
  • @BarretoFreekhealer you can constraint as much as you want .. if you set the rotation via code it doesn't matter .. also in general in order to not break the physics and collision detection you shouldn't do **any** transformation directly via the `Transform` component! Rather use `Rigidbody.MoveRotation` in `FixedUpdate`! – derHugo Jul 16 '21 at 17:43
  • @BarretoFreekhealer, rhe maximum degree measure of rotation of the y axis is 360 degrees, and I want to constrain all axes, not just the y, I read this line again: newPos = new Vector3 ( Random.Range (-10f, 10f), Random.Range (0f, 10f), Random.Range (-10f, 10f)); And I realized that it constrains the y-axis only, but I need all axes constraints, how to do this? – Arina Aznauryan Jul 16 '21 at 17:55
  • sorry? you want it constraint? i think you have some confusion there. that line of code does not constraint anything just sets a random number between specified values every t time – Barreto Jul 16 '21 at 18:31