moving plattform with rolling ball

I’m a Unity noob, so please bear with me. I have tried perusing the documentation and gotten halfway to where I want to be.

I’m trying to build a simple “rolling ball on a moving platform” game, where the platform can rotate along the X and Z axis. The ball should then roll according to the rotation of the platform.

I have a platform (mesh, rigidbody, mesh-collider) and a ball (sphere, rigidbody, sphere collider)

I disabled “use gravity” for my mesh so that it doesn’t drop down :slight_smile:

I have some code that moves the mesh

var rigidBody : Rigidbody;
var rotateSpeed = 18000;

function FixedUpdate () {
	var xRotate =  Input.GetAxis( "Vertical") * rotateSpeed;
	var zRotate = Input.GetAxis( "Horizontal") * -rotateSpeed;
	xRotate *= Time.deltaTime;
	zRotate *= Time.deltaTime;
	print(xRotate);
	rigidBody.AddRelativeTorque( xRotate, 0, zRotate);
}

and that seems to work. However, the weight of the ball forces the platform down. My next step would be to add a “addForce(Vector3.up * n)” call that counteracts the downwards movement, but I think that I’m missing something and that this could be handled much easier…

any ideas?

thanks
jc

You could simply use a hinge joint. Hinge joints also have motors so you can make it rotate without a single line of code and it wont be pushed down by objects on top of it.

Hmm - I can see how this could work, but I “think” that this would limit me to handling movement in the two axis I want to rotate my platform around to move in sync. (Assuming that the motor is working both axes at the same time)

I’d like to have the two axes move independently (through user input) - is this possible?

You can set this up using two rigidbodies and 2 hinge joints one for each axis, one of them connecting to the other.

What about just setting Is Kinematic for the platform?

Thanks Joachim - I got that set up (a small cube attached to nothing) and the platform attached to the cube with a hinge joint each. Setting the motors to turn gives me the desired individual rotation along the two axis. Now I’m trying to control the motors from a script:

var joint : GameObject;

function FixedUpdate () {
	var motor = joint.hingeJoint.motor;
	var x = Input.GetAxis( "Vertical");
	motor.targetVelocity = x * 20;		
}

but nothing moves. I have tried to change the targetVelocity and the force of the motor without any difference…

function FixedUpdate () {
   var motor = joint.hingeJoint.motor;
   var x = Input.GetAxis( "Vertical");
   motor.targetVelocity = x * 20;      
}

You are never assigning back the motor value. You can either write:

joint.hingeJoint.motor.targetVelocity = x * 20;

or

   var motor = joint.hingeJoint.motor;
   motor.targetVelocity = x * 20;      
// assign the motor to the hinge joint
joint.hingeJoint.motor = motor;

Just a small note, it is usually more convenient to just expose the property of the object you are referencing directly. Eg.

var theHinge : HingeJoint;
theHinge.motor.targetVelocity = x * 20;

This way your script gets simpler faster, you can drag&drop the game object in the same way, but Unity will only allow the drag if there actually is a hinge joint in the game object.

ah - got it! I was assuming I get a reference to the motor and that I can manipulate that directly. Now it works as it should - save for some oscillations - but I guess that’s part of the physics of the scene that now are at work!

Thanks for your fast help!

You can use a higher motor force (It wont affect the target Velocity) to avoid speed up speed down when adding more weight on it.

Can you change the direction of gravity in Unity (ie. as you can with Havok)?

If so, you could just enable gravity and rotate its directional vector (and the main camera) with user input. That way, it would look like the platform was rotating and everything would work as you want.

Sure, just alter Physics.gravity.

Then if you want custom gravity on a particular rigidbody, it’s really just a 3 or 4 line script with rigidbody.AddForce()

-Jon

Been there, done that, and it works well :slight_smile: And you have to rotate the lights too. I just parent them all together.

That’s exactly how the powerup (“Tilt” is the only one for now) works in my Clockwork 360 game. Hold Z to try it:

http://adamsi.com/clockwork

My intent was to make the whole game world tilt without the needlessly complex effects of shoving all the rigidbodies along with it, and most importantly, without changing the coords/rotation of any object! That way my other scripts are entirely unaffected by the “magic tilt.” Visually you can’t tell the difference. The tilt will even knock the central wall down properly (if the wall is weakened enough).

Woah, Clockwork360 looks pretty cool!

thanks for the additional tips. I’m just getting my feet wet with Unity and there is so much to learn…

Clockwork 360 looks awesome, and the playing around with the gravity vector seems to do exactly what I need.

Sadly my vector geometry skills are over 20 years old, so I’m having trouble getting the “rotate the gravity vector” working. Could some kind soul post some code with the necessary magic incantations of Quaternions and Vector3 objects?

And what’s that stuff with parenting?

Parenting means the “child” object is attached to the “parent,” like a character’s hear would be parented to the body. When you transform the parent, all children stay attached and get transformed too.

So when I wanted all my lights and my camera to rotate together around a fixed center, I just made an empty Game Object (from the menu) and then attached them all to that object. When I rotate that, the lights and camera all rotate and stay together, making it seem like the world is tilting. (I didn’t bother changing the blob shadow yet.) You choose an object’s parent just by dragging the child object onto the parent in the list.

It’s up to you how you want to set the angles involved, but this line should let you set gravity to any direction–zero in this case but it could be anything:

Physics.gravity = Quaternion.EulerAngles(0,0,0) * Physics.gravity;

Thanks again - I’m slowly figuring out these things. I got the gravity vector rotating (with help from the guys on the IRC channel), next step is the camera rotation to match it.

It seems, that the solution always ends up a lot simpler than I imagine it to be - but it takes time to learn about all the possible options.

You’re welcome. Be sure to attach the camera to a GameObject at world center–then you can rotate via the GameObject instead of around the camera’s own center which isn’t what you want.

duuh - of course, that makes a lot of sense. I was trying to figure out the translation of the camera on a sphere and then figure out the new rotation of the camera…

I find that working with Unity needs quite a bit “out of the box” thinking. First one thinks, that - due to the physics engine - one just can model the world and everything will be taken care of. Then it turns out, that it’s much easier to work outside the “real-virtual” world… Will need some time to get adjusted :slight_smile:

Right - got it working (it looks and feels ok…) Here’s the code for your amusement…

var go: Transform;

function FixedUpdate () {
	var z = Input.GetAxis("Vertical");
	var x = Input.GetAxis("Horizontal");
	
	var grav = Vector3(0, -9.81, 0);
	var xAngle = x * 10;
	var zAngle = z * 10;
	var q1 = Quaternion.AngleAxis( xAngle, Vector3.forward);
	var q2 = Quaternion.AngleAxis( zAngle, Vector3.left);
	 
	
	Physics.gravity = q1 * q2 * grav;
	go.transform.rotation = Quaternion.identity;
	go.transform.Rotate( -zAngle, 0, xAngle );
}