- Home /
Collision with drag object
I'm trying to set up an effect of hitting one object with another using an explicit drag by the user. I have 2 spheres in my scene. The first sphere I have coded drag behavior, which works great. The 2nd sphere is the object I want to hit. I've applied colliders and rigid bodies to both and the effect is somewhat working, but the problem is that the 2nd sphere is only getting pushed out of the way and not bouncing or reacting with the energy of the drag. I have a physics material applied to the 2nd sphere with high bounce, which I have tested using gravity bouncing off a ground plane; it is very bouncy.
My guess is that the physics engine is not taking into account the drag velocity of the 1st sphere, so when the collision occurs it is treating it like a static object. How can I overcome this? I'd like the interaction to be fully controllable by the drag, from a gentle nudge to a hard smack.
Any ideas? Thanks.
Answer by roamcel · Sep 13, 2011 at 06:48 PM
I reckon that your drag routine alters the TRANSFORM of the draggable sphere, right? By doing this, you're 'hacking' the physics, since the 'only' way to interact with a fully physics-driven object (to achieve bounces and all those other physical effects you expect) would be to apply FORCES to it, rather than translations of its transform.
The solution would be to implement a script that creates a joint on the mouse's 3d position, to which you attach the sphere, which will thus behave as a demolition crane's ball.
so basically:
- click the scene, create a joint
- attach the sphere to the joint
- drag around and smash stuff!
Voila.
That works, thanks! I created a new cube object and added Fixed Joint to it and moved over the drag script. The 1st sphere I left as a rigid body only (no drag) and connected it to the fixed joint.
I ended up dropping the Fixed Joint and ins$$anonymous$$d in my script I use rigidbody.$$anonymous$$ovePosition(), in place of transform.position. This caused the drag movement to affect the rigidbody velocity.
Answer by _Yash_ · Oct 06, 2015 at 05:55 PM
Its a video of what accepted answer is saying. https://www.youtube.com/watch?v=ZfjVR-0ZFHU
Your answer
Follow this Question
Related Questions
Unwanted jittery behavior 2 Answers
Objects with colliders going through walls and each other. 3 Answers
Terrain collision problem 1 Answer
Collision Enter one of the objects ? 1 Answer