- Home /
How to have an object attach to my player when collided
When my player walks into this object, in this case a cube, I need it to simply attach to the player as if they're carrying it. This is what I've done so far.
var blockTransform : Transform;
var playerTransform : Transform;
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Player"))
{
blockTransform.transform.parent = playerTransform.transform;
}
}
If Tim's answer works for you, please accept it as correct. Otherwise, provide more information about your problem or describe why his solution doesn't work for you.
Answer by FearofWonderland · Dec 18, 2013 at 08:35 PM
I've got the solution now. To start with, the character controller is too large so no collision can be detected. Then, this code...
Put this on the player:
#pragma strict
var blockTransform : Transform;
var playerTransform : Transform;
var holdSlot : Transform;
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Block"));
{
blockTransform = collision.transform;
blockTransform.transform.parent = transform;
blockTransform.transform.position = holdSlot.transform.position;
}
}
The block gets translated to infront of the player, or where ever the holdSlot is placed automatically when the player touches it.
I hope your actual code is properly indented. If not, I would hate to work with code base, and you'd probably find that I formatted it all. :)
I do not actully know what you are meaning AND it doesent work for me, Is It Javascript Or C#? I had put the script in the holdSlot GameObject (E$$anonymous$$PTY) but when the holdSlot touched the cube named Block it didnt do anything, Replying would be helpful.
Answer by TimBorquez · Dec 17, 2013 at 10:57 PM
you could have an empty game object as a child of the player and make a variable for it like holdSlot, then move your object to the spot like
blockTransform.transform.position = holdSlot.transform.position;
i would in most cases make picking up stuff happen from code on the player not from the thing you pick up though
I altered the code to look like this.
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Block"))
{
blockTransform.transform.parent = playerTransform.transform;
blockTransform.transform.position = holdSlot.transform.position;
}
}
But it still isn't working, I don't know what I could be doing wrong.
umm did you make a variable for your hold slot object and set it in the inspector?
well i see that you're using a name to detect the right object, did you make sure the object is named correctly
also check to see if it's even going into the collision function with a Debug.Log();
Nope, no kind of collision being detected.
#pragma strict
var blockTransform : Transform;
var playerTransform : Transform;
var holdSlot : Transform;
function Start () {
}
function Update () {
}
function OnCollisionEnter (collision : Collision)
{
if(collision.transform.name == ("Block"));
{
Debug.Log("yes");
blockTransform.parent = playerTransform.transform;
blockTransform.transform.position = holdSlot.transform.position;
}
}
Your answer
Follow this Question
Related Questions
place a object on the position where the object is colliding with the terrain. 1 Answer
Collision with a coin 0 Answers
Unparent object on collision? 3 Answers
Lowering impact between 2 objects 1 Answer
Attaching objects on collision 1 Answer