Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by oliver-jones · Nov 16, 2010 at 02:52 AM · rigidbodygravitycubestick

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Nov 16, 2010 at 02:31 PM 0
Share

Okay, Ill go ahead an try this (Im a Javascript person) Thanks

avatar image SharpCoder · Apr 07, 2016 at 09:15 PM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GamingTopTen · Oct 17, 2017 at 12:10 AM 1
Share

6 years too late, but good one! Can you make code that deletes system32 on a game over?

avatar image
0

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; }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

OnMouseDown() tracking bad at 3D box collider 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges