Last active
January 22, 2016 19:05
-
-
Save bobmoff/c869b4852e475d6c722b to your computer and use it in GitHub Desktop.
Bouncy wall physics behaviour for Cocktail Cruise
This file contains 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 UnityEngine; | |
using System.Collections; | |
public class Wall : MonoBehaviour { | |
Vector2 DesiredLocalPosition; | |
Quaternion DesiredRotation; | |
Transform DesiredTransform; | |
Rigidbody2D Body; | |
void Awake() { | |
DesiredLocalPosition = transform.localPosition; | |
DesiredRotation = transform.rotation; | |
Body = GetComponent<Rigidbody2D>(); | |
DesiredTransform = new GameObject("Desired Transform").transform; | |
DesiredTransform.position = transform.position; | |
DesiredTransform.rotation = transform.rotation; | |
} | |
void FixedUpdate() { | |
Body.AddForce((DesiredLocalPosition - (Vector2)transform.localPosition) * Time.fixedTime * Settings.I.WallPositionStrength); | |
float rotationDiff = Vector3.Cross(DesiredTransform.up, transform.up).z; | |
float angle = Vector2.Angle(DesiredTransform.up, transform.up); | |
// If angle diff less than 30 degrees, start applying less force depending on how close to desired rotation it is | |
float forceAngleModification = 1; | |
if (angle < 30) { | |
forceAngleModification = angle / 30; | |
} | |
if (rotationDiff > 0) { | |
Body.AddTorque(Time.fixedTime * -Settings.I.WallRotationStrength * forceAngleModification); | |
} | |
else if (rotationDiff < 0) { | |
Body.AddTorque(Time.fixedTime * Settings.I.WallRotationStrength * forceAngleModification); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment