In my Game there is a “hold” button that makes the player “carry” an object which the camera is raycasting on: on the first click it lifts the object (and the player can move around with the object ‘carried’ in front of him), and on the next click it drops it. i made a script of two functions for both methods - and the button has a reference for both of them in the inspector’s ‘Button script’ “on click()” menu (which i’ve seen that it is able to add a bunch of functions and not just one).
perhaps i don’t fully understand the mean of this “on click()” menu, but the result is when i hit the button, it does both functions one by one (lifts the object and than drops it right away) in just one click.
here is my script:
public GameObject mainCamera;
bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
void rotateObject() {
carriedObject.transform.Rotate(5,10,15);
}
void carry(GameObject o) {
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.identity;
}
public void pickup() {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.transform.parent.GetComponent<Pickupable>();
if(p != null) {
Debug.Log ("ray has detected an object");
carrying = true;
carriedObject = p.gameObject;
//p.gameObject.rigidbody.isKinematic = true;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
carry(carriedObject);
}
}
}
public void dropObject() {
if (carrying == true) {
carrying = false;
Debug.Log ("Dropped object");
//carriedObject.gameObject.rigidbody.isKinematic = false;
carriedObject.gameObject.GetComponent<Rigidbody> ().useGravity = true;
carriedObject = null;
}
}
}
i should probably mention that this script was not originally written by me, i’ve made a few changes in it in order to make it compatible with touch input. originally the first line in the functions “pickup()” and “dropObject()” was “if(Input.GetKeyDown (KeyCode.E))” which determines if the key e was pressed (and i assume that the on click() method supposed to put it by). i tried to convert it to that button on click() method but it doesn’t seem to work seperatly. what would you suggest to do?