Added Constrained Rotation
This commit is contained in:
parent
f5940f31c0
commit
f767c26200
11
Unity-Files/Assets/Examples/Defender/Defender.unity
generated
11
Unity-Files/Assets/Examples/Defender/Defender.unity
generated
@ -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
|
||||
|
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 * -1) && 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