I'm currently working on a script that can pick up an item, and drop it with the click of the left mouse button. I'm also planning on addint rotating of an item and some icons to display whenever i do one of those actions.
I'm currently very new to this, so i might be trowing myself out in the depths. But i would like to try.
Here's my code:
public class PickUp : MonoBehaviour {
public Transform onHand;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(1)){
this.transform.position = onHand.position;
}
}
void OnMouseDown () {
GetComponent<Rigidbody>().useGravity = false;
this.transform.position = onHand.position;
this.transform.parent = GameObject.Find("Player").transform;
}
void OnMouseUp () {
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
}
}
So far it kind of works.. I've some trouble picking up my object, it does not always let me. I have to click a couple of times, before it actually gets a hold on the object. When it does, the objects starts flying upwards for some weird reason i do not understand. I still have a hold on it, i can still walk around with it and as soon as i let go it falls down.