Tank turret rotation while tank is moving help
Ok so I'm making a tank and it has a turret that turns to look at the enemy but there are a couple problems I run into. Normally I would put the turret as a child of the tanks body so it can move relative with the body as it moves around but rotating the turret when parented to the body makes the turret resize so it still fits inside the parented body (See TankIncorrect Image). Next I unparented the turret from the body and I got my desired effect it rotates normally but it doesn't stay connected to the body as the body moves away (See TankMoving Image). I've tried using a fixed joint on the turret to keep it on the body but when I want the turret to turn it wont because its fixed to the body. Is there a way to allow the turret to rotate while its using a fixed joint? Thanks any ideas are appreciated!!
There is no script that goes with this all I did to test that was use the rotate tool at the top left
I think i have a quick fix for this. Create a new EmptyGameObject, and make the Turret a child of it. Then make the EmptyGameObject a child of the tank and see if it work.
The problem may be that you used turret as the parent of all the other things inside it, and turret IS a "model". Empty gameobjects doesn't have any mesh, so they can't have their design messed up. As only the parent's scale is changed, not the childs, it should work.
Answer by Glurth · Dec 04, 2016 at 04:29 AM
Create a new EmptyGameObject, and make the Turret a child of it. Then make the EmptyGameObject a child of the tank and see if it work.
Close, just think you have the parenting mixed up a bit.
Use an empty game object, with a scale of (1,1,1). You make this empty object the "tank position", and make the tank body a child of the "tank position" object, AND the turret a child of the object.
Now you can move and rotate the "tank position", scale the body, and rotate the turret, all separately.
In general, it gets confusing to have a scaling transform as parent of a transform with a rotation, so to avoid it, I do the above almost every-time I have a scaling transform.
Ive figured out how to allow the turret to rotate separately already but it just wont stay connected to the body and move with the body if its not a parent of the body. Ive tried making a gameobject and putting it into the body also but that doesn't work either (See TankIncorrect2). The turret doesn't want to move with the body because its not connected to it and if it was the scale is all messed up even if it is in a empty gameobject. It works fine set up like the image of TankCorrect2 shows without the empty gameobject with the turret but just wont move with the tankbody because it not connected to it. (Please look at the pictures below they should help you see the parenting I tried) Thanks for helping me with this, I don't see a way around this but any feedback is appreciated
tankcorrect2.png is exactly what I was talking about. Important to remember though: do NOT move or rotate the tank body, rather move & rotate the "tank position" object only. Only SCALE the tank body. (this is because the body's position & rotation, RELATIVE to the "tank position", (localPosition and localRotation) does not change .) Similar, but different; for the turret, do NOT change it's position, (relative to "tank position") just it's rotation. (at least 'till you blow it up!)
This is the big difference between "position" and "localPosition" in a transform: the localPosition is always RELATIVE to the parent. "position" is a combination of the parents position, and the child object's localPosition. Only when an object has no parent are localPosition, ad position the same.
Ive figured it out you also have to put the nav mesh agent on the empty gameobject or it will only move one thing but not the other thanks for helping!!
Your answer
Follow this Question
Related Questions
Get rotation of a randomly rotating turret on a moving tank (to Spawn missiles in that direction) 2 Answers
Rotate tank turret using quaternions 0 Answers
when my character rotates the controls of the initial rotation stay and not change with it 0 Answers
Object rotation problem 1 Answer
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers