Let me see if I can explain this clearly.
I have a platform linked to the top of a sphere. The sphere is a rigid body that can roll around the environment, but at the same time I want the platform to stay in place and “hover” at the top of the sphere. The hard part is that I also want both to be able to collide with the environment and act as one “solid” vehicle. When the platform collides with something I want it to affect the sphere as if they’re linked together, but the platform doesn’t inherit rotation.
I’ve tried a myriad of ways to do this and it’s fairly simple to get the platform to stay with the sphere without rotating (I created a script that simply resets the rotation to identity on update), but getting physics involved on the platform as well as following the sphere is tricky.
Can anyone provide some guidance? Is this even possible?
Thanks!
Well, the simple answer is to put physics code in the OnCollisionEnter() function that does what you want done to the sphere.
Awesome, that got me a bit closer. I have the platform staying with the ball, but now I need to handle collisions correctly.
I’m thinking that I need to transfer collision information from the platform, to the ball so they act as one cohesive whole.
I’m trying to transfer velocities, but that’s not working so well. How do I transfer collision information between two objects to keep them synced?
I’ll post my code once I’m done so hopefully this can help someone else.
In what areas do you need them to be synced?
X velocity, y velocity, etc…
Yeah. I’ve been thinking about it and maybe I’m over-analyzing it. What I’m doing shouldn’t be that different from a car with wheels. Imagine a car with one wheel, and it’s a sphere so it can turn in any direction.
When a wheel collides with a rock, it pushes the car up (in my case, the platform). And when the car collides with a wall, the wheel stays with the car.
I want it to work the same way, but without suspension. The “wheel” stays exactly with the car.
Maybe transferring velocities is the way to do it, or maybe it’s not?
Should I look into wheel colliders? Would that work for a sphere?
So you want the Platform to always stay upright?
The direction of the forward-Vector should be the direction of the last “roll-direction” of the sphere, correct?
Then have a sphere-rigidbody and plane be seperate objects, the platform as child of the sphere (this will make the rigidbody of the sphere take into account collisions of the platform).
And apply this script to the platform, which keeps it on top of the sphere, upright.
var sphere : Transform;
var lastPos : Vector3;
var yOffset : float;
function Awake()
{
lastPos = sphere.position;
}
function FixedUpdate()
{
//// Set new position
transform.position = sphere.position + Vector3.up*yOffset;
//// Calculate look-rotation
// Vector from old position to new position
var diffVec = sphere.position - lastPos;
// Ignore y-Axis movement.
diffVec.y = 0;
// If the sphere has bearly / not moved, don't change the rotation
if(diffVec.sqrMagnitude > 0.00001)
{
// Make platform look in the direction of the motion. up is always world up
transform.rotation = Quaternion.LookRotation(diffVec, Vector3.up);
}
// Update oldPos
oldPos = sphere.position;
}
EDIT:
Just tested it. Make sure that the platform has a volume of 0 (make it a plane or a cube with height 0). Else the Sphere will look like its drunk 
Very interesting and educational, thanks much! I’ll check it out tomorrow!
Ok thanks Der Dude, I had a solution similar to yours, but you added the direction look. It was actually good reference for vector math in Unity and it will help a lot down the road.
I’m still having trouble getting the collision to transfer from child to parent (platform to ball). Here is the code I have right now (I’m not using look direction though, I want the mouse to control that, that’s not a problem…)
var ball_target:Transform;
function Update () {
transform.position = ball_target.position;
transform.rotation = Quaternion.identity;
}
function FixedUpdate () {
Physics.IgnoreCollision(transform.collider, ball_target.collider);
}
function OnCollisionEnter () {
ball_target.rigidbody.velocity = transform.rigidbody.velocity;
}
I’m not sure if transferring the velocity is what I want, or if I’m even doing it right. I’m getting really close. When the platform hits an object it is always pushing in the negative X direction for some reason though.
The investigation continues…
I think the discrepancy is that I’m setting the position of the platform to the ball, as well as wanting to calculate the collisions of the platform and pass them back to the ball.
Circular reference?
Aha! Using a configurable joint to link the two instead of setting the position to match via javascript works great. Now they both have physics, and the collisions are “shared”.