- Home /
Collision Detection When Picking Up GameObjects
Good Morning, Afternoon, Evening ... Night :)
I happened to stumbled across a rather head-scratching issue here at the moment. I scripted up a brand new script that allows the main player to pick up and interact with certain gameobjects. What this essentially translates to is that the specified gameobject is parented to the player at a specific local position, and will even follow all camera motions/movement.
One odd issue does arise from this however. In that though the specified gameobject remains a rigidbody throughout these interactions, still includes collisions, operates under a fixedUpdate function and the unity time has been set accordingly to increase collision detection. It doesn't collide with other gameobjects once parented to the player. The player movement essentially overrides all possibility of any legitimate collision.
So what occurs is that the specified gameobject just floats through all the environmental Gameobjects that are present. When in reality what I'd personallly wish to occur is that it collides with an object, remains steadfast and ceases the player the from moving any further (or rotating the camera) in particular direction.
And that's partially why I'm here tonight. So any thoughts, criticisms, light slander or useful tips would be much appreciated Thank you :)
The code by nature is a relative mess,
hence why I didn't originally show it,
though the following should (hopefully) be those more relevant lines of code:
var artifact : GameObject;
var character : GameObject;
var artifactX : float;
var artifactY : float;
var artifactY : float;
// Establishes The Set "GameObject" As Gravityless
artifact.GetComponent(Rigidbody).useGravity =false;
// Parents The Artifact To A Set Parent Location - "Character"
// The Parent Being An Empty GameObject Parented To The $$anonymous$$ain Player With The $$anonymous$$ouselook (Y) Script Attached
artifact.transform.parent = character.transform;
// Sets The "Vector3" Location Between The "GameObject" + Parent
var artifactLocation = Vector3 (artifactX, artifactY, artifactZ);
artifact.transform.localPosition = artifactLocation;
I don't think that it's a good idea to have rigidbodies as children. The code that you've presented doesn't contain any errors/problematic solutions.
Have a look at this: http://answers.unity3d.com/questions/62497/-you-should-never-have-a-parent-and-child-rigidbod.html
If you're actually having children rigidbodies then to solve it you've got different options:
A. make a script that keeps the object in place, by applying force. There is an example of this in the standard assets provided by Unity.
B. attach a joint to the rigidbody
hope this helps :)
It wasn't quite the solution I was seeking,
Though you've most definitely been a fair help all the same, So I have reason to believe I owe you my thanks all the same. You've certainly introduced me to the knowledge surrounding rigidbodies. And the joint system has proven quite useful all the same.
So though you may well have sent me down the wrong internet rabbithole there, it proved to be relatively beneficial all the same. So thank you so much, as it sure helped! :)
Answer by Pawscal · May 13, 2014 at 06:58 PM
Before parenting, i would try to change the Rigidbody's property "isKinematic" to true. The object should still interact with other physics enabled GameObjects, but you should be able to move it around as you want. Then, when you drop it dowm set back "isKinematic" to false.
Hope this helps!