- Home /
Turret rotation with a pivot?
I have a unit with a gun gameobject attached to its body. I run into a problem when I tell the gun to lookup at the gray square on the black gameobject (See Picture).
The gun looks up at the gameobject but it also moves itself off of the unit gameobject. I want the side of the gun that is connected to the gameobject to be anchored to the body. I've tried using joints to fix this problem but still not sure if this would be the best solution. Any help would be appreciated!
Code used to make gun turret look at gray "LookAt" gameobject:
Quaternion LookObjRot = Quaternion.LookRotation (LookAt.transform.position - controller.Gun.transform.position);
controller.Gun.transform.rotation = Quaternion.Slerp (controller.Gun.transform.rotation, LookObjRot, Time.deltaTime * 5);
Answer by Eno-Khaon · Dec 28, 2018 at 07:33 PM
The easiest way to do this is to create an empty parent object for the turret to act as its pivot point.
Then, rather than rotating the turret itself, you rotate that empty GameObject:
controller.GunPivot.transform.rotation = etc.
Yes this works but, is there any way to make the end of the gun that is connected to the unit stay there while the end of the gun moves freely around? @$$anonymous$$o-$$anonymous$$haon
Wherever the gun's pivot point (parent object) is set to, that will be the point that the gun rotates around when you rotate the pivot point (parent object) itself.
If that's not what you're looking for, then could you provide some more clarity on exactly what it is you're trying to do? I'm not really sure why it wouldn't "stay there" in this case.