- Home /
Make A Cube 'Sticky'
Hello,
I want the ability to make a cube 'sticky' so it would stick to walls or other objects.
Example: Maybe this cube is a sticky cam, or C4 or something around that. How would I go about making this cube have gravity (rigidbody) all the time, and then when it comes in contact with a wall, the gravity is killed (causing it to 'stick') -- Unless any of you have another idea?
Better yet, when the user walks up to a wall and presses 'Fire1' the cube is thrown/placed - or at least 'activates' the 'stick'.
Thanks, Ollie
Answer by duck · Nov 16, 2010 at 01:43 PM
You can dynamically create a fixed joint when the object collides with something, like this:
C#
using UnityEngine; using System.Collections;
public class StickyObject : MonoBehaviour { void OnCollisionEnter(Collision c) { var joint = gameObject.AddComponent<FixedJoint>(); joint.connectedBody = c.rigidbody; } }
or Javascript:
function OnCollisionEnter(c : Collision) {
var joint = gameObject.AddComponent(FixedJoint);
joint.connectedBody = c.rigidbody;
}
You'd place the script on to the moving object, and when that object touches any other rigidbody in your scene, it will create a fixed joint that attaches it to that object.
Okay, Ill go ahead an try this (Im a Javascript person) Thanks
Hi! If both objects has this "sticky" script, how to prevent it from double collisions? For example, cube1 is near cube2, and both will execute OnCollisionEnter which means that there will be 2 fixed joints and both objects would be connected to each other. Is there any way to prevent it?
Answer by Reverbdude · Jul 04, 2011 at 03:27 PM
I have a game where i do this! Heres the script i used (JS)
function OnCollisionEnter ()
{
Destroy (rigidbody);
}
attatch that to the projectile.
6 years too late, but good one! Can you make code that deletes system32 on a game over?
Answer by Proclyon · Nov 16, 2010 at 01:48 PM
You can try something as simple as this, a 1 touch is a permanent stick. Quick and easy, and definatly not a pro script. It's just a quick fix ;) like using spit for glue
private bool hookedToSomething = false;
void Update() { if (hookedToSomething) { transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z); }
}
void OnControllerColliderHit(ControllerColliderHit cch) { //You detected a collision with the object hookedToSomething = true; }
Answer by oliver-jones · Nov 16, 2010 at 07:13 PM
Right,
Its getting there, few problems though:
I have a model of C4, which basically, when the user presses 'e' it Initiates a rigidbody (which is a copy of the C4)
The model script is:
var C4 : Rigidbody;
function Update() { if (Input.GetKeyDown ("e")) { if (C4) { var Throw : Rigidbody = Instantiate(C4, transform.position, transform.rotation); }
}
}
And the Copy (the one that is being thrown) is:
function Start () {
rigidbody.AddRelativeForce (Vector3(1 * 100,-1 * 100,0));
}
function OnCollisionEnter(c : Collision) { var joint = gameObject.AddComponent(FixedJoint); joint.connectedBody = c.rigidbody; }
What I would like to do now is for the collider on the C4 copy to ignore the Player Controller and the C4 copy and original and anything with the tag "test"
Hope that makes sense,
Regards, Ollie
Your answer
Follow this Question
Related Questions
Gravity is not working no matter what 0 Answers
Making 3D Objects Stick On Other Objects Like A Grid? 2 Answers
hit.normal cube 0 Answers
Making a cube fall without rigidbody 1 Answer