void Update() { //checking if the distance is close enough for grabbing game object distance = Vector3.Distance(item.transform.position, tempParent.transform.position); //initialize holding is false if (distance >= 1f) { isHolding = false; } if (isHolding == true) { item.GetComponent <Rigidbody>().velocity = Vector3.zero; item.GetComponent <Rigidbody>().angularVelocity = Vector3.zero; item.transform.SetParent(tempParent.transform); //adding right mouse click as throwing objects while users grabbing objects if (Input.GetMouseButtonDown(1)) { item.GetComponent <Rigidbody>().AddForce(tempParent.transform.forward * throwForce); isHolding = false; PickupTrackData tdd = new PickupTrackData(); tdd.objName = item.name; tdd.time = Time.time; tdd.type = 2; // throwing = 2 listw.data.Add(tdd); if (td != null) { td = null; } } } else //if isHolding is false the game object stays the same place { objectPos = item.transform.position; item.transform.SetParent(null); item.GetComponent <Rigidbody>().useGravity = true; item.transform.position = objectPos; } if (distance <= 1f) { //new feature key press E to push objects if (Input.GetKeyDown(KeyCode.E)) { item.transform.SetParent(tempParent.transform); item.GetComponent <Rigidbody>().AddForce(tempParent.transform.forward * throwForce); PickupTrackData tdd = new PickupTrackData(); tdd.objName = item.name; tdd.time = Time.time; tdd.type = 2; // throwing = 2 listw.data.Add(tdd); if (td != null) { td = null; } } } }
//left mouse click enable user to grab void OnMouseDown() { if (distance <= 1f) { isHolding = true; item.GetComponent <Rigidbody>().useGravity = false; item.GetComponent <Rigidbody>().detectCollisions = true; if (td == null) { td = new PickupTrackData(); td.objName = item.name; td.time = Time.time; td.type = 1; // pickup = 1 listw.data.Add(td); } } }
//turning holding is false void OnMouseUp() { isHolding = false; td = null; }