0

I want to spawn blocks which have a rotation speed after pressing the space button. I don't think the following code works because it is spawning a new GameObject each time.

public GameObject blockPrefab;
void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        Vector3 randomSpawnPosition = new Vector2(Random.Range(Player.screenHalfWidth, -Player.screenHalfWidth), 0);
        Vector3 randomSpawnRotation = new Vector3(0, 0, Random.Range(0, 360));
        GameObject block = Instantiate(blockPrefab, randomSpawnPosition, Quaternion.Euler(randomSpawnRotation));
        block.transform.parent = transform;

        // How do I set a rotation velocity here?
        block.transform.Rotate(new Vector3(0, 0, Random.Range(0, 30)* Time.deltaTime), Space.Self);
    }
}

I am unsure what to do as all examples say to use Rotate() but as far as I can tell that will never work in this situation.

userqwert
  • 2,193
  • 5
  • 28
  • 50
  • In general it's always good to check the API. [`Rotate`](https://docs.unity3d.com/ScriptReference/Transform.Rotate.html) is a rotation transformation of the object about a certain angle in all three axis which is done **once**. If you want a continuous rotation either use it in `Update` as explained already below or use a `Rigidbody` and give it a certain [`angularVelocity`](https://docs.unity3d.com/ScriptReference/Rigidbody-angularVelocity.html)(If you are not using Physics this would be performance overkill though) – derHugo Nov 05 '19 at 05:49
  • You should consider using `FixedUpdate()` instead of `Update()` for such physics related things. `Update()` runs once per frame, whereas `FixedUpdate()` has its own timestep an removes jitter. [Read more](https://stackoverflow.com/questions/34447682/what-is-the-difference-between-update-fixedupdate-in-unity). – jhoepken Nov 13 '19 at 06:07

2 Answers2

3

Separate your instantiation from your rotation. You are doing both in the same "if" statement. Create a separate script for the instantiation and one for the rotation. Attach the rotation script to your "Block" prefab.

If you were to create a new script for rotating the prefab, here's an example of a Rotator.cs Update() method:

//...
void Update()
{
    transform.Rotate(new Vector3(0, 0, Random.Range(0, 30)* Time.deltaTime), Space.Self);
}
//...
Erik Overflow
  • 2,220
  • 1
  • 5
  • 16
  • This looks really promising, but all of the cube instances have the same rotation - is there a way to do it on a per instance basis? – userqwert Nov 05 '19 at 23:22
  • I don't understand. Are you asking how to make all cubes have the same rotation? Or are you asking to make them all rotate independently? @userqwert – Erik Overflow Nov 05 '19 at 23:44
  • I want them to all rotate randomly and independently around their z-axis, at the moment they all use the same rotation speed and direction after being spawned. – userqwert Nov 06 '19 at 10:06
  • This code allows each object to rotate independently if you move the Random.Range to the start method in the Rotator.cs script and reference it. – Erik Overflow Nov 06 '19 at 15:15
2

If your blocks have rigid bodies attached, you can get a random rotation like this:

    cube.GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere;
Lou Garczynski
  • 624
  • 3
  • 10