How to create a Unity3D object that sticks to surfaces when thrown:
I will assume you are starting with a fresh scene, and have a little familiarity with Unity3D.
1) Firstly create a floor surface in your scene.
2) Next, drag a First Person Controller into the scene window, and tag it as “Player” — you’ll probably need this if you make your scene more advanced later on.
Also, make a surface/wall that you will be throwing at, and tag that to ‘target‘. Add a collider to it if it doesn’t already have one.
Here is my basic scene thus far:
3) Introduce your throwable 3D object to the scene, by dragging it into the scene window.
Give it a collider, if it lacks one (Component > Physics).
Give it rigidbody, if it lacks one (Component > Physics).
4) Now, we’ll make a JavaScript ‘ThrowableScript‘, and explain it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#pragma strict function Start () { } function Update () { } function OnCollisionEnter(collision : Collision) { Debug.Log(collision.collider.tag); if(collision.collider.tag == "target") { rigidbody.isKinematic = true; } } |
Drag this script onto your throwable object in the Hierarchy panel.
This script simply analyses collisions on the thrown object, looking for a collider with the tag ‘target‘. Upon detecting such a collision, the rigidbody is changed to ‘kinematic’ so that it immediately stops the physics motions it was displaying. A debug line was added – you might find that useful for error checking what objects were being touched, if you make your scene more complex.
5) Next we’ll need to make this a spawnable object. Make a prefab: right-click (Create > Prefab) in the Project panel, and drag the throwable object from the Hierarchy panel to the newly created prefab icon in the Project panel. The object should now have blue text in the Hierarchy window.
6) Delete the throwable object from the scene hierarchy. Don’t worry — you have created a prefab in the Project panel, and can clone that object from code now.
Here is what my prefab’s Inspector settings look like:
7) Now we’ll make a JavaScript ‘ThrowObj‘, with a brief explanation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#pragma strict var throwableObj : Rigidbody; private var speed = 30.0; function Start () { } function Update () { if(Input.GetKeyDown(KeyCode.E)){ var instantiatedThrowable : Rigidbody = Instantiate(throwableObj, transform.position, transform.rotation); instantiatedThrowable.velocity = transform.forward * speed; } } |
Functionally, the code, attached to your player, will wait for you to press ‘E‘ and then it will spawn and throw the prefab from that position. There is a variable ‘throwableObj‘, for your throwable prefab, and you’ll also see a ‘speed‘ variable: experiment with ‘speed‘ to make it suitable to your needs.
8) Expand your First Person Controller in the Hierarchy panel, and drag the ‘ThrowObj‘ script onto its Main Camera object. Why the Main Camera? Well, we’ll be using its current rotation (your viewing angle) to help determine the angle the spawned object should fly out at.
9) You’ll need to drag your thowable prefab from the Project Panel onto the Main Camera’s ‘ThrowObj‘ script’s ‘throwableObj‘ variable in the Inspector.
Here is what my Main Camera’s Inspector settings look like:
10) Test the scene out. Here’s what I got:
With this basic system, you can do more obvious things that don’t require ‘sticky’ objects, like generating rockets. However, don’t instantiate machine gun bullets like this, use raycasts instead. Prefab objects spawned at that rate could cripple your memory resources.
You may have a case where you want to stick the object to a moving target — in this case the Kinematic setting will probably leave the object hovering in midair. What you should do in this case is to parent the thrown object to the targeted object upon collision. That way, the thrown object will use the parent’s position to orient itself. You can get a reference to the targeted object through the collision data.
When you have spawned your objects, you will notice them accumulating in your Hierarchy panel: in a subsequent article you will be shown a simple way to get these objects to auto-delete after a certain time period.
Pingback: A Unity3D object that sticks to surfaces | VISUALS+CODE
Thank u very much
I have been looking for this God knows how long and finally I found it
thanks
your my savior