Search code examples
c#unity-game-enginegame-enginegame-physics

unity 3d make object follow player


I want to make a robot on unity3D. I want to make the gripper of the robot when collide with object attach the gripper. So the object will follow the gripper.

What will be added to this script in order to make some thing like this?

private Rigidbody gripper;

void Start() 
{
   gripper_part01 = GetComponent<Rigidbody>();
}

void Update() 
{
   if (Input.GetKey("a")) 
       gripper.AddForce(transform.forward * 100);
}

void OnCollisionEnter(Collider obj1) 
{
   // how to make obj1 follow the gripper
}

Solution

  • It can be done by various methods. But the simplest would be making the obj1 child of gripper as soon it collides.

    code will look some thing like this

    void OnCollisionEnter(Collider obj1) 
    {
       // how to make obj1 follow the gripper
       obj1.transform.parent = gripper.transform;
    }