cube carry sphere

This is my Prototype link I’m working now to teach myself Unity
Now you can see the cube follow mouse moving and there is a sphere, my next mission is trying do the following:

| When cube collide sphere, sphere jumps to the top of cube and follow cube moving |

please, I need some start up guides …

As for start up guides: “3D platform Game” and and “Car Tutorial” are what a lot of people recommend, and what I started with. You
can find them on unity’s Tutorials page here. Theres also “VTC - Introduction to Game Development Using Unity 3D Tutorials”, thats an absolutely amazing video set that really got me going… can be found here

As for making the sphere react to the cube, making a script in the sphere with an OnCollisionEnter function would probably be easiest. Example of OnCollisionEnter here.

Basically you just have a global variable, of the type transform, and then in your collision function get the transform of the object that hit it. yourcubetransform = Collision.transform; Then just reset the sphere’s transform in an update function to follow the cubes transform, some thing like transform.position = yourcubetransform.position + Vector3(0,1,0);

Where the Vector3(0,1,0) is just to place it above the cube.

Please, can you give me a code, I’m beginner in programming

sure:

private var FirstHit : Transform; // This is the global variable I talked about, it has to be out of the function because that makes it global, it has to be global so it doesn't get deleted the next frame (variables created in a function get deleted for memory sake). Its private so you don't mess with it in the inspector, you don't need it there. It will be what stores your cube, or what ever collides with it first, so it knows what to follow.

function OnCollisionEnter(Collision collision)
{
    if(FirstHit==null) // Check to make sure you haven't already collided with some thing. (if you haven't the variable will = null because theres nothing in it)
        FirstHit = collision.transform;
}

function Update()
{
    if(FirstHit != null) // Make sure FirstHit is not null, otherwise this next line wont work (you can't follow some thing that doesn't exist)
        transform.position = FirstHit.position + Vector3.up; // Every frame place the object that this script is in 1 unit above FirstHit (assuming of course that it isn't null)
}

Of course if you wanted to have the sphere get “dropped off” when you collided again, then you could just remove the if(FirstHit==null) (and the comment that follows, make sure you don’t get an error with that) then it will follow what ever object it collided with last. So you could smack your cube into it, then it will follow the cube. Then if you carry it over to another cube tall enough to hit it, it would stick to it and stop following your cube.

but i fix this line:

function OnCollisionEnter(Collision : collision)

TO

function OnCollisionEnter(collision : Collision)

but when i attach this code to the sphere, nothing happened, look my attached package

686993–24696–$test12-09.unitypackage (57.4 KB)

The colon you put in is good, typo on my part.

Though I forgot the OnCollisionEnter function requires that both objects doing the colliding have to have ridgidbodies, why this is I don’t know, seems like collision function should be usable without ridgidbodies. In any case, it’s a problem easily solved, just add a ridgedbody to both the sphere and cube (in the component menu under physics), then in the inspector just disable gravity so they don’t fall, and under “constraints” in the ridgidbody, constrain every thing by checking those 6 check boxes (that way they don’t collide with each other and start spinning and flipping all over the place).

Now when i pressed right click IN ANY PLACE, the sphere become child to the cube in runtime, How can make sphere child to the cube by pressing right click BUT when sphere collide cube:
The following code attached it to the cube:

var newMissile : Transform;

function Update () {
if (Input.GetButtonDown("Fire2"))
{
var newTransform = newMissile.transform;
newTransform.parent = transform;
}
}

Unity - Scripting API:

I’ve been tinkering with picking up objects and I’d like to recommend Raycast as an option.
OnCollision + Keypress/Down/Up in my experience is kind of clumsy as you have to slam your character into the object AND press the object at the exact time that the collision takes place.
Raycast on the other hand does it based on the distance you are from the object…I suppose OnTrigger could work as well but I found that to be not as tight as Raycast.
B