Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Created September 25, 2021 21:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsheffield/68f658878f4cefc9b1b229bd3cc937e8 to your computer and use it in GitHub Desktop.
Save samsheffield/68f658878f4cefc9b1b229bd3cc937e8 to your computer and use it in GitHub Desktop.
Spinning GameObject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
// Set the velocity of rotation (positive numbers go counterclockwise, negative numbers go clockwise)
public float spinVelocity = 10f;
private Rigidbody2D rb2d;
void Start()
{
// Get a reference to this gameobject's Rigidbody2D component
rb2d = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
// Update the rigibody2d's angular velocity property every fixed update cycle
rb2d.angularVelocity = spinVelocity;
}
}
Here is a bonus Unity example for 2D Game Design F21. Let me know what else you need!
======================================================================================
SPINNING A RIGIDBODY2D
Full example: Spin.cs
Important:
1. Your GameObject needs a Rigidbody2D component
3. You might want to set Position Constraints in the Rigidbody2D component
4. If you want this Rigidbody to be influenced less by collision, set its Mass property to something VERY HIGH
// Update the rigibody2d's angular velocity property using a float variable every fixed update cycle
rb2d.angularVelocity = spinVelocity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment