- Home /
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
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 :)
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$$
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).
Answer by Mexallon · Jun 01, 2013 at 11:08 PM
Hej there. That should be easy.
Make sure your player has a Character Controller or Rigidbody component in the scene.
Add any form of collider to the boxes you want to stick to the player
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.
Seems like you were faster than me. The rigidbody is not needed though. A character controller would work the same.
Ok thanks for the reply. Haven't done alot with the character controllers yet.
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
).
Your answer
Follow this Question
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