- Home /
Objects with colliders going through walls and each other.
I have a script that enables the player to drag objects around in the scene. But even though these objects have colliders and Rigidbodies, they still go through walls and each other. Here is my script:
using UnityEngine;
using System.Collections;
public class mouseDrag : MonoBehaviour {
void OnMouseDrag()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen ));
}
}
Answer by Gruffy · Nov 27, 2013 at 01:41 PM
Basically i think from what you`ve said is that your rigidbody is updating later than your repositioning of the mouse, which would be about right...
You see RigidBody updates in FixedUpdate (once for every 2 frame updates)(this is Unity`s special physics update method) OnMouseDrag is updating every "Update" method frame.(so , it is updating twice as fast as your rigidbody)
Basically you are reaching a point in space using mouse and applying that translation to your object, your problem is rigidbody is trying to catch up with everything you just did inside a method that is twice as slow as "Update".
Suggestions: (not recommended) Either rethink your approach.
(Recommended) In Unity 3D, there come a script in the standard assets packages that can be imported by right clicking the mouse inside your Project browser and selecting "Import New Asset"
scroll down the list and find the Unity scripts packages.
Inside your project`s "Standard Assets" folder view in Unity3D, once imported, is a script named DragRigidBody.js.
This will ultimately be the droid your looking for! Apply the script in the same way as you have done so to your previous attempt. Read the the comments in the script to see what it`s doing and how its handling what you were trying to achieve
btw, This has been answered before Here
Take care and good luck.
If this helped to solve it for you , then mark it as answer if you can so others can benefit from it too :)
Gruffy
:)
I have replaced mousDrag.cs with DragRigidbody.js, and it does nothing when I drag. What do I do?
O$$anonymous$$, I worked it out, the parts can't be set to kinematic. Is there a way I can keep the collision in, but not push away the object it collides with (In this case another building block)
Answer by Subhajit-Nath · Nov 27, 2013 at 01:41 PM
When using Rigidbody, don't manipulate transform directly. Use rigidbody.MovePosition instead. Hope this helps.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.MovePosition.html
Answer by ashjack · Nov 28, 2013 at 08:55 PM
Sorry to keep bothering you, but I figured that out too. I added into the script that disables mouse orbit when a brick is being dragged a bit that makes bricks kinematic if they are not being dragged. I don't need any more help now, and thanks for showing me the script!
Your answer
Follow this Question
Related Questions
Stop sword from distorting character animation 3 Answers
Getting a sword in place while attacking an enemy 3 Answers
Why object goes sometimes through walls by adding force ? 2 Answers
Trouble with Physics.IgnoreCollision 0 Answers
Unwanted jittery behavior 2 Answers