From f767c26200f12f1d1058d10c894d4b2be609077b Mon Sep 17 00:00:00 2001 From: Philip White Date: Mon, 1 Nov 2021 00:16:09 +0000 Subject: [PATCH 1/2] Added Constrained Rotation --- .../Assets/Examples/Defender/Defender.unity | 11 ++-- .../Scripts/Movement/RotateConstrained.cs | 51 +++++++++++++++++++ .../Movement/RotateConstrained.cs.meta | 11 ++++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs create mode 100644 Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta diff --git a/Unity-Files/Assets/Examples/Defender/Defender.unity b/Unity-Files/Assets/Examples/Defender/Defender.unity index 0484dad..9b85df1 100644 --- a/Unity-Files/Assets/Examples/Defender/Defender.unity +++ b/Unity-Files/Assets/Examples/Defender/Defender.unity @@ -380,8 +380,8 @@ GameObject: m_Component: - component: {fileID: 267901558} - component: {fileID: 267901560} - - component: {fileID: 267901559} - component: {fileID: 267901561} + - component: {fileID: 267901559} - component: {fileID: 267901557} m_Layer: 0 m_Name: Cannon @@ -426,14 +426,15 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 267901556} - m_Enabled: 0 + m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: b3c7bb76e85354455909e3f696d0e13a, type: 3} + m_Script: {fileID: 11500000, guid: e63701da40cba6c459b22784a509abe1, type: 3} m_Name: m_EditorClassIdentifier: rigidbody2D: {fileID: 0} - typeOfControl: 1 - speed: 25 + typeOfControl: 0 + speed: 5 + constraintRotationAmount: 0.3 --- !u!50 &267901560 Rigidbody2D: serializedVersion: 4 diff --git a/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs new file mode 100644 index 0000000..c5f62d8 --- /dev/null +++ b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs @@ -0,0 +1,51 @@ +using UnityEngine; +using System.Collections; + +[RequireComponent(typeof(Rigidbody2D))] +public class RotateConstrained : Physics2DObject +{ + [Header("Input keys")] + public Enums.KeyGroups typeOfControl = Enums.KeyGroups.ArrowKeys; + + [Header("Rotation")] + public float speed = 5f; + + [Header("Movement Constraint")] + [Range(0, 1)] + public float constraintRotationAmount = 0.3f; + + private float spin; + + + // Update gets called every frame + void Update() + { + // Register the spin from the player input + // Moving with the arrow keys + if (typeOfControl == Enums.KeyGroups.ArrowKeys) + { + spin = Input.GetAxis("Horizontal"); + } + else + { + spin = Input.GetAxis("Horizontal2"); + } + } + + + // FixedUpdate is called every frame when the physics are calculated + void FixedUpdate() + { + //Freezes the objects rotation if it hits the constrained bounds + if ((this.transform.rotation.z >= constraintRotationAmount && spin <= 0f) || (this.transform.rotation.z <= (constraintRotationAmount * -1) && spin >= 0f)) + { + rigidbody2D.freezeRotation = true; + } + else + { + rigidbody2D.freezeRotation = false; + // Apply the torque to the Rigidbody2D + rigidbody2D.AddTorque(-spin * speed); + } + } +} diff --git a/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta new file mode 100644 index 0000000..5c8adfa --- /dev/null +++ b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e63701da40cba6c459b22784a509abe1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 5088c2dfdcc6b415b2f4a9b12a8e331d19cf8ba0 Mon Sep 17 00:00:00 2001 From: Philip White Date: Mon, 1 Nov 2021 00:42:01 +0000 Subject: [PATCH 2/2] update Constrained Scripts Optimizations --- .../Assets/Scripts/Movement/MoveHorizontalConstrained.cs | 2 +- Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Unity-Files/Assets/Scripts/Movement/MoveHorizontalConstrained.cs b/Unity-Files/Assets/Scripts/Movement/MoveHorizontalConstrained.cs index 21bf38b..3bd2683 100644 --- a/Unity-Files/Assets/Scripts/Movement/MoveHorizontalConstrained.cs +++ b/Unity-Files/Assets/Scripts/Movement/MoveHorizontalConstrained.cs @@ -43,7 +43,7 @@ public class MoveHorizontalConstrained : Physics2DObject void FixedUpdate() { //Sets the objects velocity to 0 if it hits the constrained bounds - if ((this.transform.position.x >= constraintOffsetXPosition && moveHorizontal >= 0f) || (this.transform.position.x <= (constraintOffsetXPosition * -1) && moveHorizontal <= 0f)) + if ((this.transform.position.x >= constraintOffsetXPosition && moveHorizontal >= 0f) || (this.transform.position.x <= -constraintOffsetXPosition && moveHorizontal <= 0f)) { rigidbody2D.velocity = new Vector2(0f, 0f); } diff --git a/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs index c5f62d8..a0e99d5 100644 --- a/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs +++ b/Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs @@ -37,7 +37,7 @@ public class RotateConstrained : Physics2DObject void FixedUpdate() { //Freezes the objects rotation if it hits the constrained bounds - if ((this.transform.rotation.z >= constraintRotationAmount && spin <= 0f) || (this.transform.rotation.z <= (constraintRotationAmount * -1) && spin >= 0f)) + if ((this.transform.rotation.z >= constraintRotationAmount && spin <= 0f) || (this.transform.rotation.z <= -constraintRotationAmount && spin >= 0f)) { rigidbody2D.freezeRotation = true; }