This is a start for my pickup script, its used to pickup objects in the world. WHen picked up it should be added as a child to the gameobject which the script is attached to. It does not work correctly, the object is attached, but its also transformed, its scale changes
public class PickupObject : MonoBehaviour {
private readonly List<GameObject> collidings = new List<GameObject> ();
private GameObject pickedUpObject;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
print (collidings.Count);
if (Input.GetMouseButtonDown (0) && pickedUpObject == null && collidings.Count != 0) {
print("Picking up");
pickedUpObject = collidings.First ();
collidings.Clear();
pickedUpObject.transform.parent = this.transform;
} else if (Input.GetMouseButtonUp (0)&& pickedUpObject != null) {
print("Dropping");
pickedUpObject.transform.parent = null;
pickedUpObject = null;
}
}
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Movable" && pickedUpObject == null) {
collidings.Add(other.gameObject);
}
}
void OnTriggerExit (Collider other) {
if (other.gameObject.tag == "Movable") {
collidings.Remove(other.gameObject);
}
}
}