I am using the code below to pick up and drop an object. I want it so when I press E, the object is picked, and when I don’t, it is dropped at my current location. But this code doesn’t seem to pick it up. My object is tagged cube, and the picked up gameobject is set to the cube too. I am new to javascript, and your help would be appreciated. The code:
var hit:RaycastHit;
var pickedUpObject:GameObject;
if(Input.GetKey("e")){
if(Physics.Raycast(transform.position,transform.forward,hit,100)){//the order of the parameters might be wrong.
if(hit.gameObject.tag=="cube"){ //i used a tag to see whether the object cn be picked up, you can use another method that may suit you better
pickedUpObject=hit.gameObject; //we use this to determine whether an object is picked up by the player. If it's not null, then the player is doing so.
hit.gameObject.transform.parent=transform; //attach the object to the camera so it moves along with it.
hit.gameObject.transform.position=transform.position-transform.forward; //might need changing as it's untested.
}
}
}
else if(pickedUpObject!=null){ //if player is not holding E but was picking up an object last frame
pickedUpObject.transform.parent=null; //drop the object
pickedUpObject=null; //and nullify the object pointer
}