Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Yasser_E · Mar 04, 2016 at 11:57 AM · c#unity 5unity5attachstick

Make GameObject “attach” properly?

This script makes a cube "stick" to whatever it collided with. The problem is that when it's going at relatively high or medium speeds (or when the device itself is slow), the cube tends to "get a bit inside" what it collided with and then stick to it. What changes do I have to make to fix this?

In order for this script to work, one GameObject must have bool _sticksToObjects = true; and the other bool _sticksToObjects = false;

I have tried turning the Rigidbody's Collision Detection mode to either Continuous or Continuous Dynamic

I think my script depends on frame rate. That may be where the problem lies.

Normal "Attach":

see here

Abnormal "Attach":

see here

 Rigidbody _rigidBody;
 Transform _meshTransform;
 bool _sticksToObjects = true;
 
 public Transform _stuckTo = null;
 protected Vector3 _offset = Vector3.zero;

 void Awake()
 {
     GameObject CubeMesh = GameObject.FindWithTag ("CubeMesh");
     GameObject Cube = GameObject.FindWithTag ("Cube");

     _rigidBody = Cube.GetComponent<Rigidbody> ();
     _meshTransform = CubeMesh.GetComponent<Transform> ();
 }

 void LateUpdate() 
 {
     if (_stuckTo != null)
     {
         transform.position = _stuckTo.position - _offset;
     }
 }
 
 void OnCollisionEnter(Collision collision)
 {
         if (!_sticksToObjects) {
             return;
         }
             
         _rigidBody.isKinematic = true;
     
         // Get the approximate collision point and normal, as there
         // may be multipled collision points
         Vector3 contactPoint = Vector3.zero;
         Vector3 contactNormal = Vector3.zero;
         for (int i = 0; i < collision.contacts.Length; i++) {
             contactPoint += collision.contacts [i].point;
             contactNormal += collision.contacts [i].normal;
         }
     
         // Get the final, approximate, point and normal of collision
         contactPoint /= collision.contacts.Length;
         contactNormal /= collision.contacts.Length;
     
         // Move object to the collision point
         // This acts as setting the pivot point of the cube mesh to the collision point
         transform.position = contactPoint;
     
         // Adjust the local position of the cube so it is flush with the pivot point
         Vector3 meshLocalPosition = Vector3.zero;
     
         // Move the child so the side is at the collision point.
         // A x local position of 0 means the child is centered on the parent,
         // a value of 0.5 means it's to the right, and a value of -0.5 means it to the left
         meshLocalPosition.x = (0.5f * contactNormal.x);
         _meshTransform.localPosition = meshLocalPosition;
     
         if (_stuckTo == null || _stuckTo != collision.gameObject.transform) {
             _offset = collision.gameObject.transform.position - transform.position;
         }
     
         _stuckTo = collision.gameObject.transform;
     }

Here are some screenshots of the Unity editor:

Screenshot 1: i.stack.imgur.com/ivA6F.png

Screenshot 2: i.stack.imgur.com/9XGpv.png

Screenshot 3: i.stack.imgur.com/XP8G2.png

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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Properly attach to a GameObject after collision? 0 Answers

How attach C# script by argument? 3 Answers

.... does not contain a definition for ... 1 Answer

Accessing GameObjects when changing scenes (levels) 1 Answer

Do we still need to avoid foreach loops? 3 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