- Home /
Rotating a 2d object
In my space shooter game I want to rotate my asteroids two-dimensionally.
I put together this script, but now the asteroid rotates three-dimensionally:
using UnityEngine;
using System.Collections;
public class RandomRotater : MonoBehaviour
{
public float rotate;
void Start ()
{
rigidbody.angularVelocity = Random.insideUnitSphere * rotate;
}
}
How can I do it so it simply just rotates a random amount of degrees pr. second?
Answer by Pyrian · Apr 30, 2014 at 04:51 PM
If the game is 2D, you might want to be using a Rigidbody2D instead of a Rigidbody. The angularVelocity property of a Rigidbody2D is a float in degrees per second - exactly what you need:
rigidbody.angularVelocity = Random.value * rotation;
If you're set on using a 3D rigidbody, though, you're going to want to set just a single type of rotation, normally Z:
rigidbody.angularVelocity = new Vector3(0.0F, 0.0F, Random.value * rotation);
Ahhh, I see! And yeah, I'm sticking with the 3D rigidbody, as I've done this my whole game.
Thank you so much for the answer pal. It fixed it :-)
Your answer
Follow this Question
Related Questions
Arm Rotation toward Cursor Script Help 1 Answer
How to make flat object(Unity 2D) 1 Answer
2D rotation around a object 4 Answers