Created
September 25, 2021 21:03
-
-
Save samsheffield/68f658878f4cefc9b1b229bd3cc937e8 to your computer and use it in GitHub Desktop.
Spinning GameObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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