# How to create a 'flying balance'

Hello,

I have a game object which can be moved in any direction (on a 2D plane - up down/left right). It reacts to gravity and has collision. Its something like a spaceship. What I want to do, is apply a Hinge Joint where the object has a pivot point and if it hits another object, it rotates and then returns back to its original orientation (using Spring- I guess). So this would look like a “flying balance”. I already have a movement script, but as soon as I attach the Hinge Joint, everything stops working. Is there a way of applying this effect? I’m doing this in C#.

I’m attaching a screenshot to show this more clearly.
The object moves in a X - Y world. I want to rotate it along its Z axis (following the red arrows).

Thank you!

At least could someone tell me if this is possible or not? I would appreciate it very much. THANKS!!!

Yes, I’m sure it’s possible, what code have you tried such that things stop working?

Well, I’m noob here as well and have no programming skills and trying to figure it out as well. So far I found this script somewhere (not sure in what tutorial). It works, but I cannot figure out how to make Z rotation. Anyone could help?

Szymon.

///////////
var target : Transform;
var distanceMin = 10.0;
var distanceMax = 15.0;
var distanceInitial = 12.5;
var scrollSpeed = 1.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;
private var distanceCurrent = 0.0;

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

distanceCurrent = distanceInitial;

// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

function LateUpdate () {
if (target) {
x += Input.GetAxis(“Horizontal”) * xSpeed * 0.02;
y -= Input.GetAxis(“Vertical”) * ySpeed * 0.02;
distanceCurrent -= Input.GetAxis(“Mouse ScrollWheel”) * scrollSpeed;

distanceCurrent = Mathf.Clamp(distanceCurrent, distanceMin, distanceMax);
y = ClampAngle(y, yMinLimit, yMaxLimit);

var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distanceCurrent) + target.position;

transform.rotation = rotation;
transform.position = position;
}
}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}