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 ));
}
}
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
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!