Abdou023
Abdou023

Reputation: 1664

unity3d - Cannot move a parent empty gameobject

I have an empty game object which is a parent of several childs that create parts of a tank, The problem is I wrote a script to make that parent object move according to mouse, but when I apply it to it does nothing, whilst when I apply it to one of the children it works perfectly, but of course that child gets detached from the parent. My question is why is this script not working on the parent which is an empty object ?

void OnMouseDrag () {

        float distance = transform.position.z - Camera.main.transform.position.z;

        Vector3 position = new Vector3 (Input.mousePosition.x , Input.mousePosition.y , distance);

        position = Camera.main.ScreenToWorldPoint(position);

        Vector3 target = new Vector3 (position.x, transform.position.y, transform.position.z);

        transform.position = target;
    }

Upvotes: 0

Views: 1283

Answers (1)

Max Yankov
Max Yankov

Reputation: 13277

Let me quote the documentation:

OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.

Since your game object is "empty", I gather that it doesn't contain a Collider component, and thus the OnMouseDrag doesn't get called.

Upvotes: 3

Related Questions