- Home /
Simple Parenting Question
Hi everyone! :D
Ok so I made a simple trigger script where when the space ship enters the trigger it can pick up the object by pressing and holding F (at least I think I did the input code right), and when its released the object drops.
Now this script is on all pickupable objects, and the object checks if the object entering and exiting the trigger is tagged "Player" or "ShipLoaded" and executes the PickUp() and Drop() functions.
My question is how to in those codes make the object holding that script child of the object with one of those tags in the PickUp() function?
#pragma strict
var canPickUp : boolean = false;
function OnTriggerEnter(coll:Collider)
{
if(coll.gameObject.tag == "Player" || "ShipLoaded")
{
canPickUp = true;
}
else
{
canPickUp = false;
}
}
function OnTriggerExit(coll:Collider)
{
canPickUp = false;
}
function Update()
{
if(Input.GetKey("f") && canPickUp)
{
PickUp();
}
else if (Input.GetKeyUp("f") && canPickUp)
{
Drop();
}
}
function PickUp()
{
//parenting code goes here
}
function Drop()
{
//unparenting code goes here
}
Hi, define what do you mean with "pick up". How do you want it to be picked up ? Everybody can see the picked object, it's on a rope or something, or the object just disappears on f hold, and spawns again when you release the f button.
By pick up I mean hold, not disappear just hold, parented to the object with tags "Player" or "ShipLoaded" and moves the way its parent moves.
When I clicked on this question, I thought it was going to say something like: "What advice should I give my teenage daughter about homework..." heh :)
Answer by JoaquinRD · Aug 17, 2013 at 02:51 PM
Use transform.parent = coll.transform;
Try this:
#pragma strict
function OnTriggerStay(coll:Collider)
{
if(tranform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.GetKey("f"))
{
transform.parent = coll.transform;
}
}
function Update()
{
if (Input.GetKeyUp("f"))
{
tranform.parent = null;
}
}
By the way, using the or (||) operator will not work with two strings, such as:
if(coll.gameObject.tag == "Player" || "ShipLoaded")
You have to compare each of them separately:
if(coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded")
Thank you for that! I didn't know that, and I forgot I can put everything in the if statement even tho I know how. xD
Anyway, it didn't work at first but then I changed it a bit so it works kind of now. When I press F it gets parented but it doesn't move exactly like its parent. In fact it barely moves at all. Did I misunderstand the concept of parenting? I thought parenting means the child inherits all of its parents attributes, including script functions for movement, for example.
Here is the new version of the code:
#pragma strict
function OnTriggerStay(coll:Collider)
{
if(transform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.Get$$anonymous$$ey("f"))
{
rigidbody.useGravity = false;
rigidbody.mass = 1;
transform.parent = coll.transform;
Debug.Log("Picked up!");
}
}
function OnTriggerExit(coll:Collider)
{
transform.parent = null;
Debug.Log("Dropped it D:");
rigidbody.useGravity = true;
rigidbody.mass = 5;
}
function Update()
{
if (Input.Get$$anonymous$$eyUp("f"))
{
transform.parent = null;
Debug.Log("Dropped it D:");
rigidbody.useGravity = true;
rigidbody.mass = 5;
}
}
Ah, I did not consider that the pickup might use physics. Ins$$anonymous$$d of setting useGravity and mass, simply set is$$anonymous$$inematic to true when picked up and false when dropped. This will make sure that the pickup is securely attached because it will ignore all physics and just follow its parent.
Also, I don't think you'll need OnTriggerExit.
Ok we're almost there, its working now, there's one more thing left now. When, while holding the object, the object collides with something it just goes through it. I attempted to make it unparent when it does but nothing happens its still goes trough. :S
#pragma strict
function OnTriggerStay(coll:Collider)
{
if(transform.parent == null && (coll.gameObject.tag == "Player" || coll.gameObject.tag == "ShipLoaded") && Input.Get$$anonymous$$ey("f"))
{
rigidbody.is$$anonymous$$inematic = true;
transform.parent = coll.transform;
Debug.Log("Picked up!");
}
}
function Update()
{
if (Input.Get$$anonymous$$eyUp("f"))
{
transform.parent = null;
Debug.Log("Dropped it D:");
rigidbody.is$$anonymous$$inematic = false;
}
}
function OnCollisionEnter(coll:Collider)
{
transform.parent = null;
rigidbody.is$$anonymous$$inematic = false;
}
This is happening because its collider is s trigger. OnCollisionEnter will only be called if the collider is not a trigger.
Your answer
Follow this Question
Related Questions
How to put two game objects together and disable rigidbody on a child? 1 Answer
Player object still gets destroyed even when shields up 1 Answer
i cant get an object to become child of an other JS 1 Answer
OnCollisionEnter(Collision Other) setActive affecting parent 1 Answer
Is calling OnCollisionEnter from multiple scripts bad? 0 Answers