Control Ship and Gun Separately

I currently have ship orbiting a planet controlled by my left joystick. I have a gun (which is a child of ship in the hierarchy) that is controlled by the right joystick. When the ship isn’t moving the gun can shoot in the direction that the joystick is pointing just fine. Now when I start controlling the ship, it also rotates the gun as well which makes the shooting direction change, even though I didn’t move the right joystick.

Does anyone know how I can have the two objects rotate independently?

Let me know if you need more info.

Instead of having the turret be a child of your ship, you should have the ship be a child of your turret, and attach a kinematic rigidbody to it (the ship). Rotating the turret should be pretty straightforward now that it’s the parent, but your movement code should not alter the turret’s rotation. Now, in order to properly rotate the ship, you can attach this to the ship gameobject (which, again, should be a child of the turret):

var rotationToHold:Quaternion;
if(rigidbody.velocity.magnitude>0.1){//if we are moving
transform.rotation=rigidbody.velocity; //make the ship's rotation face its movement
rotationToHold=transform.rotation; //store this frame's rotation
}
else{
transform.rotation=rotationToHold; //if not moving, keep the rotation we had on the last frame that we were moving
}

Here is a visual representation:

-Turret (moves with left analog stick, rotates with right analog stick, movement does not affect rotation)
   -ship (does not rotate based on input but based on velocity and is a child of turret)

Edit: Perhaps it is a fairly complex solution that would require you to radically change the way your objects are set up. I am also pretty sure that there is a much more simple way to do this that i am overlooking at the moment.

Edit 2: How can i be so silly, ignore the setup and use the code that i gave you for the turret, but instead of velocity you should check whether there is right thumbstick input.

if(/*multiple Input.GetAxis checks here to see if there is right thumbstick input*/)
rotationToHold=transform.rotation;
else{ //if no right thumbstick input
transform.rotation=rotationToHold;
}

I am keeping my original answer up in case it suits you better though.

Your answer didn’t really address my question, but I’ll make a couple of suggestions. One solution is to not make the gun a child of the ship, but to have the gun move with the ship. An easy way to accomplish this relationship is to:

  • Place an empty game object at the center of the ship (don’t make it a child).
  • Place the gun where it needs to be in relationship to the ship
  • Make the gun a child of the empty game object
  • Add a script to the empty game object that moves it to the same position as the ship every frame.

This will leave the rotation of the gun the same in world space, but you will see the gun point at new object as the ship is translated. If you want the gun to remain on target even when the ship moves, you can use Transform.LookAt(), but I’m not sure how you would marry joystick position to LookAt() movement.