- Home /
Question by
Lucifk · Jun 22, 2016 at 05:10 AM ·
gameobjectphysicsmouse-drag
How to get mouse pointer to hover over object when being dragged?
I want the game object to precisely follow the mouse while it is being dragged. When I click on my game object and drag in a direction the mouse moves ahead of the object and the object follows the direction of the mouse. I don't want this and instead prefer the mouse pointer to hover over the object when it is being dragged. I know this is easy but if you could help me I would appreciate it.
void OnMouseDrag() {
point = Camera.main.ScreenToWorldPoint
( new Vector3( Input.mousePosition.x,
(transform.position.y-Camera.main.transform.position.y),
(transform.position.z-Camera.main.transform.position.z)));
point.z = gameObject.transform.position.z;
point.y = gameObject.transform.position.y;
rigBody.AddForce (point, ForceMode.VelocityChange);
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "RightCube")
{
rigBody.velocity = Vector3.zero;
Debug.Log ("Collision detected");
}
}
Comment
Your answer
Follow this Question
Related Questions
Make a game object "carry" other 1 Answer
How to make a public game object array inside an array (or 2D array) that can drag and drop to fill? 1 Answer
2D Destroy GameObject on small parts, without having each part pre-defined 0 Answers
Instantiate a prefab and parenting it? 1 Answer
All encompassing Game Object. 1 Answer