Skip to main content
Added better formatting and example
Source Link
basklein
  • 416
  • 2
  • 9

OK, this answer may sound a bit weird, but from what you've posted it looks like you're aiming the two parts separately at the same thing. So right now the gun barrel is probably rotating very weirdly, but is in the end still looking at the target as intended.

Now the bottom part of the turret is working fine, which makes sense, since that part just looks at the target normally, but ignores any vertical rotation. However, what you really should be doing when aiming the barrel, is aiming it as if the bottom part were already rotated correctly and facing the target.

A really easy but quite hacky way to do it would be to create an invisible game object inside the turret that tracks the target 1 to 1, then have the segments of the turret rotate towards those rotation values. That way you leave all the hard stuff for Unity to figure out.

Solution 1:

The "real" way to solve this, would be to, instead of calculating the real target direction, calculate a fake direction in front of the turret. If the transform of your barrel is already in the middle of the turret, that makes it a lot easier. Then you can just set newDirection.x and newDirection.z to 0 and it should be fine. If the transform is not in the middle, well, I recommend you put it in the middle, otherwise you're going to have to do a lot of annoying calculation to figure out the right position.

The fix would be to do this:
Vector3 look = Quaternion.LookRotation(newDirection).eulerAngles;
turretXRotation.transform.localRotation = Quaternion.Euler(look.x, 0, 0);

Solution 2:

A really easy but quite hacky way to do it would be to create an invisible game object inside the turret that tracks the target 1 to 1, then have the segments of the turret rotate towards those rotation values. That way you leave all the hard stuff for Unity to figure out.

OK, this answer may sound a bit weird, but from what you've posted it looks like you're aiming the two parts separately at the same thing. So right now the gun barrel is probably rotating very weirdly, but is in the end still looking at the target as intended.

Now the bottom part of the turret is working fine, which makes sense, since that part just looks at the target normally, but ignores any vertical rotation. However, what you really should be doing when aiming the barrel, is aiming it as if the bottom part were already rotated correctly and facing the target.

A really easy but quite hacky way to do it would be to create an invisible game object inside the turret that tracks the target 1 to 1, then have the segments of the turret rotate towards those rotation values. That way you leave all the hard stuff for Unity to figure out.

The "real" way to solve this, would be to, instead of calculating the real target direction, calculate a fake direction in front of the turret. If the transform of your barrel is already in the middle of the turret, that makes it a lot easier. Then you can just set newDirection.x and newDirection.z to 0 and it should be fine. If the transform is not in the middle, well, I recommend you put it in the middle, otherwise you're going to have to do a lot of annoying calculation to figure out the right position.

OK, this answer may sound a bit weird, but from what you've posted it looks like you're aiming the two parts separately at the same thing. So right now the gun barrel is probably rotating very weirdly, but is in the end still looking at the target as intended.

Now the bottom part of the turret is working fine, which makes sense, since that part just looks at the target normally, but ignores any vertical rotation. However, what you really should be doing when aiming the barrel, is aiming it as if the bottom part were already rotated correctly and facing the target.

Solution 1:

The fix would be to do this:
Vector3 look = Quaternion.LookRotation(newDirection).eulerAngles;
turretXRotation.transform.localRotation = Quaternion.Euler(look.x, 0, 0);

Solution 2:

A really easy but quite hacky way to do it would be to create an invisible game object inside the turret that tracks the target 1 to 1, then have the segments of the turret rotate towards those rotation values. That way you leave all the hard stuff for Unity to figure out.
Source Link
basklein
  • 416
  • 2
  • 9

OK, this answer may sound a bit weird, but from what you've posted it looks like you're aiming the two parts separately at the same thing. So right now the gun barrel is probably rotating very weirdly, but is in the end still looking at the target as intended.

Now the bottom part of the turret is working fine, which makes sense, since that part just looks at the target normally, but ignores any vertical rotation. However, what you really should be doing when aiming the barrel, is aiming it as if the bottom part were already rotated correctly and facing the target.

A really easy but quite hacky way to do it would be to create an invisible game object inside the turret that tracks the target 1 to 1, then have the segments of the turret rotate towards those rotation values. That way you leave all the hard stuff for Unity to figure out.

The "real" way to solve this, would be to, instead of calculating the real target direction, calculate a fake direction in front of the turret. If the transform of your barrel is already in the middle of the turret, that makes it a lot easier. Then you can just set newDirection.x and newDirection.z to 0 and it should be fine. If the transform is not in the middle, well, I recommend you put it in the middle, otherwise you're going to have to do a lot of annoying calculation to figure out the right position.