- Home /
trouble with parenting
i want to instantiate an object, im using
if (Input.GetButtonDown("Fire1"))
Instantiate(newObject, transform.position + transform.forward * 2.0 , transform.rotation);
after instantiating i want the object to stay in front of my player while i am walking around, then when i want to drop it i will click the mouse again. i think i have to use something like:
myObject.transform.parent = gameObject.FindWithTag("player").transform;
myObject.GetComponent(Rigidbody).isKinematic = true;
i cant even get the object to stay in my view while im moving it just drops to the ground or stays in one spot. any help is appriciated thank you
possibly disable the rigidbody component when you pickup the object and enable again when you drop it.
Doing this should get rid of the gravity when you are moving it.
trouble with parenting eh?
trouble with parenting eh..
if(child.is$$anonymous$$isbehaving)
parent.ThreatenToRemovePrivilages();
else
parent.RewardBehaviorWithAffection();
I know, I'm hilarious
Answer by Bunny83 · Mar 12, 2011 at 04:19 AM
Since you only posted some snippets of your code I can't really figure out what exactly you're doing wrong, but i guess you work on the wrong object. Instantiate takes as first parameter a object that should be cloned (can be a prefab). The function returns the new created clone.
Also it's not clear to which object your script is attached. If it's attached to the player don't use FindWithTag it's not necessary. So your script could look like:
var newObjectPrefab : GameObject; // Assign object prefab to this var private var currentObject : GameObject = null;
function Update() { if (Input.GetButtonDown("Fire1")) { if (currentObject == null) { currentObject = Instantiate(newObjectPrefab, transform.position + transform.forward * 2.0 , transform.rotation); currentObject.transform.parent = transform; currentObject.rigidbody.isKinematic = true; } else { currentObject.transform.parent = null; currentObject.rigidbody.isKinematic = false; currentObject = null; // To be able to spawn another one } } }
Your answer
Follow this Question
Related Questions
Turret Limitations on a Moving Object 0 Answers
Weapon passing through colliders (objects)? 0 Answers
Boolean being constantly redefined? 2 Answers
How to set up items- triggers with rigidbodies 2 Answers
Moving child transform also moves parent rigidbody 0 Answers