- Home /
Attaching an object to a controllable rolling ball...
Hey guys,
I'm a newbie to unity and trying to work out how I can create a controllable rolling ball which has another object (like a box) move with it. The box would be placed directly above the ball and turn in the same directon when the ball is moved around. To add to this, the box would react to movement as if it were attached to the centre of the ball; so when the ball moves forward, for example, the box would slightly move back, because it is attached to the pivot point of the ball and is reacting to the forward force. The, when the ball slows down and comes to a hauls, the box then springs back to the centre position above the ball; as it it attached by a spring.
I have been playing with rolling ball demo on the unity page which is cool. Now I just want to see if I can add this box object to it. My initial thought is to create a.n object which is the child of the ball and attach the box to that. Then, add a script to the box to understand the action of the ball. I'm assuming there are various ways to approach this, but I'd love to hear your thoughts on a script/s and where and how to attach these to the given objects.
Thanks in advance for any help.
Rich
I hope that this makes sense!
Answer by mhtraylor · Apr 07, 2013 at 12:54 PM
Try using a Spring Joint attached to the box: http://docs.unity3d.com/Documentation/Manual/Physics.html. For instance, you could create an empty GameObject at the desired target height above the ball, set in script to always be at a position above the sphere, with an attached Rigidbody. Then set this empty object as the Connected Rigidbody of the Spring Joint that is attached to the box. You can adjust the Spring to get the desired movement.
Thanks. I will give this a go. $$anonymous$$uch appreciated.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
Here at Unity Answers, Answer means Solution, not Response.
Please watch : http://video.unity3d.com/video/7720450/tutorials-using-unity-answers
I have converted this to a comment for you. Also you don't have to wait for a moderator to approve a comment.
$$anonymous$$y fault. Sorry.
So, just trying to work this out:
'...you could create an empty GameObject at the desired target height above the ball, set in script to always be at a position above the sphere...'
How is this done exactly. I was assu$$anonymous$$g you make the empty gameobject the child of the ball, but that's not working.
Thanks in advance.
Not quite. Try this : Create an empty gameObject. Attach the following script :
#pragma strict
public var target : Transform;
public var offsetToTarget : Vector3 = new Vector3( 0.0, 1.5, 0.0 );
private var myTransform : Transform;
function Start()
{
myTransform = transform;
}
function Update()
{
myTransform.position = target.position + offsetToTarget;
}
Now in the inspector where it says target, drag and drop your Ball object. Now hit Play, and move the Ball object around. You will notice that the empty gameObject stays at the offset position (0, 1.5, 0) to the Ball object. Change the offset Vector3 to where you want the empty object to follow. That's it =]
That's ace. Thanks a bunch. I have a couple of other questions if you could be so kind:
I want the object that is targeting the ball to follow the direction of ball (turn it's face to the direction the ball)
Can the object have a spring motion to give the impression that it it is see$$anonymous$$gly attached with a tight spring? So when it jumps for example, the attached object has a little bounce to it?
Again thanks in advance.
Your answer
Follow this Question
Related Questions
How should I make a animate fully physics based Active Ragdoll? 0 Answers
How to use character controller to push down hinge joint properly 1 Answer
Can a character collider be affected by a rigidbody collider? 0 Answers
ragdoll head stretching 0 Answers
Ground clamping a rigidbody on slope 1 Answer