Object Clone as a child

Hey Guys I want my gun clone atached as a child of my Player, so when i rotate my view the gun is moves the same directions. Can someone help me pls.

public class FirstPersonController : MonoBehaviour
{
   
    public Transform cameraTransform;
    public CharacterController characterController;
    public GameObject normalGun;
    public GameObject deagle;

   
    public float cameraSensitivity;
    public float moveSpeed;
    public float moveInputDeadZone;

   
    int leftFingerId, rightFingerId;
    float halfScreenWidth;

   
    Vector2 lookInput;
    float cameraPitch;

   
    Vector2 moveTouchStartPosition;
    Vector2 moveInput;

   
    void Start()
    {
       
        leftFingerId = -1;
        rightFingerId = -1;

      
        halfScreenWidth = Screen.width / 2;

       
        moveInputDeadZone = Mathf.Pow(Screen.height / moveInputDeadZone, 2);

        Instantiate(normalGun, normalGun.transform.position, Quaternion.identity);

    }

Just store the new instance you created in a variable. Then you can do what you want with it.

var gunInstance = Instantiate(normalGun, normalGun.transform.position, Quaternion.identity);
gunInstance.transform.parent = transform;

The other alternative would be to use a different form of Instantiate which accepts a parent transform. See:

1 Like

Thank you very much, that helped me a lot.