- Home /
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.
The ship can turn, and the ship can move. Should the gun maintain it target for both types of movement or just if the ship turns?
Yes, so as the ship is moving freely, the gun should stay pointing/shooting in the direction that the joystick is facing. Since the ship is orbiting, that seems to be what is throwing me off haha
Answer by Professor Snake · Feb 19, 2013 at 02:00 AM
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.
Thanks! $$anonymous$$aking the ship a child of the Gun worked, and I had to apply a rotation fix script to it as @robertbu suggested... I appreciate the help!
Answer by robertbu · Feb 19, 2013 at 02:09 AM
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.
Sorry for the misunderstanding of your question, I tried this and it worked... sort of, as I move around the planet the bullet values get swapped for some reason, must be the LookAt() values I'm trying. Thanks again for the thoughts. I got it to work by making the Ship a child of the Gun as @Professor Snake's answer specified, and I created a script to fix the rotation. I'm going to have to give the answer to him this time around, although both of your answers were very helpful.