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
0
Question by BrFantana · Jun 01, 2013 at 10:49 PM · stickmagnet

Stick objects to player on contact

Hey there,

first: I'm a german graphic design student, and I'm working on my first game. So scripting is relative new to me.

The plan where I need some help: the are small cubes lying on the floor. When the player walks through the cubes, they stick to the player-model. Like if they were magnetic or burdocks.

Thanks for your help Brian

Comment
Add comment · Show 3
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 ExTheSea · Jun 01, 2013 at 11:13 PM 0
Share

So do you only want to have so if the player actually touches it they stick. Or schould they also move towards the player.

For the actual "sticking" part you could parent the cube to the player like:

 cubetransformvariable.parent = playerTransformvariable;

You could maybe use joints ins$$anonymous$$d if you want a less static "sticking":

http://docs.unity3d.com/Documentation/ScriptReference/Joint.html

http://docs.unity3d.com/Documentation/Components/class-HingeJoint.html

http://www.unity-news.de/die-unity3d-joint-typen-fixed-hinge-spring-configurable-joint/231

Btw: I'm german too :)

avatar image BrFantana · Jun 10, 2013 at 09:19 PM 0
Share

Hey Guys!

I appreciate all your answers(!), but it doesn't work anything.

I think that it won't work if the player is not a ridgid body. But if it's one, it falls trough the ground in my level. I'm sitting in front of my computer and get grey hair...

I give it a second try, and define my problem a littel bit more precise.

The idea of my game: $$anonymous$$y player ist a 3x3x3 "pixel"-cube. It rolls through my level and everytime it rolls over a pixel which is palaced in the level, this pixel sticks to one of the pixels of the player-cube. (Image for visualisation: http://tinypic.com/r/2vnqa0i/5)

Further in program$$anonymous$$g i plan, that the cubes the player rolls over, make the player heavyer (dark pixels) or lighter (bright pixels) in order to reach different areas of the level (jump higher or step on a platform which sinks down).

I know that i can't ask for a working script, but i hope you understand my problem a little bit more now.

And again: Thanks for your help!

Cheers $$anonymous$$

avatar image ExTheSea · Jun 11, 2013 at 03:55 PM 0
Share

Why should the player fall through the ground? If the player has a collider/character Controller attached to it which is not a trigger and is not on a collision layer which doesn't react to collisions with the ground it should work.

Ok so joints won't be an option in your case but simple parenting would. Parenting is not dependent on a rigidbody.

The rigidbody/character controller is just to make the cube be able to act as a physic object (In your case so that it can "roll" and doesn't just float in the air).

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mexallon · Jun 01, 2013 at 11:08 PM

Hej there. That should be easy.

  1. Make sure your player has a Character Controller or Rigidbody component in the scene.

  2. Add any form of collider to the boxes you want to stick to the player

  3. Add a (for example) C# script to the player which includes (besides start und update) this method:

      private void OnCollisionEnter(Collision c){
             c.transform.parent = gameObject.transform;
         }
    
    

that makes the cube a child object of the parent (the player) thus it moves with the player.

I hope I understand your probleme correctly and you have an idea what to do.

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 Lockstep · Jun 01, 2013 at 11:17 PM 0
Share

Seems like you were faster than me. The rigidbody is not needed though. A character controller would work the same.

avatar image Mexallon · Jun 02, 2013 at 10:48 AM 0
Share

Ok thanks for the reply. Haven't done alot with the character controllers yet.

avatar image
0

Answer by FL · Jun 01, 2013 at 11:21 PM

Make the cubes to face the player (or a point in the player body) using transform.LookAt() and, after, transform.Translate(Vector3.forward * Time.deltaTime). Only activates this (the magnetism) when the player is near, check using Vector3.Distance().

When the player is too near I suggest you to make the object stop moving and make it a parent of the player (Something like transform.parent = player.transform).

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create magnetic platform in Unity 2D ? 0 Answers

How to follow exactly a curve ? 2 Answers

Sticking object on mesh 0 Answers

Rotation with fixed position . , 1 Answer

Coin Magnet Performance Issue Mobile 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