- Home /
Pick up, drop and place tagged objects
Hi, with my script I am able to pick up and drop objects tagged as "Pick" with the E key but the bottom of half of the script does not work. What I am trying to do is allow the player to not only pick up and drop an object but place it where they choose to with the Q key, if an object has been placed it will stay in its position regardless of gravity but still contain its collider.
However, when the Q key is pressed I get the message in the console "Object placed" and I receive no errors but the object that is being held does nothing, can someone help me out with this? thanks.
EDIT
I've managed to get the picked up object to detach from the player when "Q" is pressed however the object drops to the ground since its gravity turns back on even though near the bottom of my script it sets the objects gravity to false. I found out that this is due to the code under the "else" that sets the objects gravity to true despite what any other line of code tells it to do. Is there a way I can set lines 36 & 37 so that they only initiate once when they are told to do so? I've also updated my code.
var hitObject : GameObject;
var SpawnTo : Transform;
var hit : RaycastHit;
var runOnce : boolean = false;
var pickedUp = false;
function Update(){
if(Input.GetKeyDown(KeyCode.E)){
if(Physics.Raycast(transform.position,transform.forward,hit,3)){
if(hit.collider.gameObject.tag == "Pick"){
pickedUp = !pickedUp;
}
}
}
if(pickedUp == true){
hitObject = hit.collider.gameObject;
hitObject.rigidbody.useGravity = false;
hitObject.rigidbody.isKinematic = true;
hitObject.rigidbody.detectCollisions = false;
hitObject.transform.parent = SpawnTo.transform;
hitObject.transform.position = SpawnTo.transform.position;
hitObject.GetComponent(RotateObject).enabled = true;
if(!runOnce){
runOnce = true;
print("ooonnnccceee");
hitObject.transform.localEulerAngles = new Vector3(0,0,0);
}}else{
SpawnTo.transform.DetachChildren();
hitObject.rigidbody.useGravity = true;
hitObject.rigidbody.isKinematic = false;
hitObject.rigidbody.detectCollisions = true;
hitObject.GetComponent(RotateObject).enabled = false;
runOnce = false;
}
if(Input.GetKeyDown(KeyCode.Q) && pickedUp == true){
print("Object placed");
SpawnTo.transform.DetachChildren();
hitObject.GetComponent(RotateObject).enabled = false;
pickedUp = false;
}
}
replace the last line with:
hitObject.GetComponent<RotateObject>().enabled = false;
Sorry mate this gives me a bunch of errors :/
you set :
hitObject.rigidbody.useGravity = false; hitObject.rigidbody.is$$anonymous$$inematic = true;
after dropping the object. This will just make it stay where it is, change to :
hitObject.rigidbody.useGravity = true; hitObject.rigidbody.is$$anonymous$$inematic = false;
Setting the objects gravity to false is what I want so that it stays in its place, ive updated my question and code.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054640im_/https://answers.unity.com/themes/thub/images/avi.jpg)