problem attaching gameobject to a player, help needed!

hello, i’m working to create a jetpack system, but i have a little problem attaching the jetpack to the player.

always that i attach the jetpack to the player it got some weird rotation from the current player rotation and the jetpack never got with the right rotation.

i’m using this code to attach the jetpack to the player

    void GiveJetPack(){
       GameObject parentOb = JetPackAttachLoc.transform.gameObject; 
       ActiveJetPack = (GameObject)Instantiate (JetPackPr, parentOb.transform.position, Quaternion.Euler (0, 0, 0));
       ActiveJetPack.transform.parent = parentOb.transform;
       bHasJetPack = true;
    }

there is someway to attach the object to the player without getting the player rotation to avoid weird rotation for the jetpack ?

thanks in advance for all your help.

Quaternion.Euler(0f,0f,0f) is the starting rotation you’re giving to the instantiated object, and that is is in world space. Basically you’re telling it to be at 0,0,0 irrespective to anything else.

Reset the rotation to local zero after you set the parent:

void GiveJetPack(){
       GameObject parentOb = JetPackAttachLoc.transform.gameObject;
       ActiveJetPack = (GameObject)Instantiate (JetPackPr, parentOb.transform.position, Quaternion.Euler (0, 0, 0));
       ActiveJetPack.transform.parent = parentOb.transform;
       ActiveJetPack.transform.localRotation = Quaternion.identity;
       bHasJetPack = true;
    }

Also, I would make bHasJetPack a property, rather than a field, so you never have to reset it:

public bool HasJetPack {
    get {
        return ActiveJetPack != null;
    }
}

thanks for your help, that worked!

1 Like