Search Unity

Tying to Rotate a Transform around One Axis to Face its Target.

Discussion in 'Scripting' started by Deleted User, Feb 17, 2019.

  1. Deleted User

    Deleted User

    Guest

    If you are playing a 3D game, such as Minecraft, when you walk up to an enemy, they will turn to face you. But they will do so without rolling or pitching. They will spin around there Y-Axis until they are facing the player, even if the player is above them or below them.
    I have a gun mounted to a car. I Raycast from the Third-Person camera every FixedUpdate and send the target point to the gun. I need it to pivot slowly to point at the target coordinates, without rolling. It uses two GameObjects as bones to control the model, with one controlling the yaw and one controlling the pitch of the gun.
    Everything I have tried so far has failed in one way or another, usually because they cause the gun to roll anyways.
    I have made a diagram trying to understand it, and to help outline my question:
    Axis Lock Demonstration.png
    The closer Transform represents either of the gun's pivots, and the farther one is the HitInfo.point from the Raycast. Because the terrain isn't flat, and the car that the gun is mounted to CAN roll, the guns may need to pivot to point at a target that is in a separate plane.