Merge pull request #3 from Games-Academy-Student-Work-21-22/constrained-rotation
Added Constrained Rotation Script
This commit is contained in:
commit
c2f0ddb93e
11
Unity-Files/Assets/Examples/Defender/Defender.unity
generated
11
Unity-Files/Assets/Examples/Defender/Defender.unity
generated
@ -533,8 +533,8 @@ GameObject:
|
|||||||
m_Component:
|
m_Component:
|
||||||
- component: {fileID: 267901558}
|
- component: {fileID: 267901558}
|
||||||
- component: {fileID: 267901560}
|
- component: {fileID: 267901560}
|
||||||
- component: {fileID: 267901559}
|
|
||||||
- component: {fileID: 267901561}
|
- component: {fileID: 267901561}
|
||||||
|
- component: {fileID: 267901559}
|
||||||
- component: {fileID: 267901557}
|
- component: {fileID: 267901557}
|
||||||
m_Layer: 0
|
m_Layer: 0
|
||||||
m_Name: Cannon
|
m_Name: Cannon
|
||||||
@ -579,14 +579,15 @@ MonoBehaviour:
|
|||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 267901556}
|
m_GameObject: {fileID: 267901556}
|
||||||
m_Enabled: 0
|
m_Enabled: 1
|
||||||
m_EditorHideFlags: 0
|
m_EditorHideFlags: 0
|
||||||
m_Script: {fileID: 11500000, guid: b3c7bb76e85354455909e3f696d0e13a, type: 3}
|
m_Script: {fileID: 11500000, guid: e63701da40cba6c459b22784a509abe1, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
rigidbody2D: {fileID: 0}
|
rigidbody2D: {fileID: 0}
|
||||||
typeOfControl: 1
|
typeOfControl: 0
|
||||||
speed: 25
|
speed: 5
|
||||||
|
constraintRotationAmount: 0.3
|
||||||
--- !u!50 &267901560
|
--- !u!50 &267901560
|
||||||
Rigidbody2D:
|
Rigidbody2D:
|
||||||
serializedVersion: 4
|
serializedVersion: 4
|
||||||
|
@ -43,7 +43,7 @@ public class MoveHorizontalConstrained : Physics2DObject
|
|||||||
void FixedUpdate()
|
void FixedUpdate()
|
||||||
{
|
{
|
||||||
//Sets the objects velocity to 0 if it hits the constrained bounds
|
//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);
|
rigidbody2D.velocity = new Vector2(0f, 0f);
|
||||||
}
|
}
|
||||||
|
51
Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs
Normal file
51
Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs
Normal file
@ -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 && spin >= 0f))
|
||||||
|
{
|
||||||
|
rigidbody2D.freezeRotation = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rigidbody2D.freezeRotation = false;
|
||||||
|
// Apply the torque to the Rigidbody2D
|
||||||
|
rigidbody2D.AddTorque(-spin * speed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta
generated
Normal file
11
Unity-Files/Assets/Scripts/Movement/RotateConstrained.cs.meta
generated
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: e63701da40cba6c459b22784a509abe1
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
Loading…
x
Reference in New Issue
Block a user