Skip to content

Instantly share code, notes, and snippets.

@bobmoff
Last active January 22, 2016 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobmoff/c869b4852e475d6c722b to your computer and use it in GitHub Desktop.
Save bobmoff/c869b4852e475d6c722b to your computer and use it in GitHub Desktop.
Bouncy wall physics behaviour for Cocktail Cruise
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