Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

430
Visualizações
Unity - Added extra offset in facing target maybe difference between Rotate() vs transform.rotation?

I am confused to if using Rotate() and .rotation will conflict with each other.

I am moving a transform to point at a Target automatically with .rotation and when I choose to move manually I use Rotate(). But after I stop manual control and use the automatic mode the transform will be offset with the manual movement I had done. So the Rotate() is adding offset. The manual control no matter what is accurate. The automatic targeting is added an offset after using the manual control.

Here is the code for the Manual Control.

xDif = Input.GetAxis("Mouse X") * mouseSensitivity.x;
yDif = -Input.GetAxis("Mouse Y") * mouseSensitivity.y;
float zDif = 0; // <-- gets changed with buttons
transformToMove.Rotate(new Vector3(yDif, xDif, zDif));

Here is the code for Automatic Targeting

/// Returns true if the transform has been successfully oriented to fire at the current target.
private bool TrackTarget(out Vector3 hitPosition, out float hitTime)
{
    hitPosition = Vector3.zero;
    hitTime = -1.0f;
    
    if (!IsTargetValid(currentTarget))
    {
        state = TransformState.NoTarget;
        return false;
    }

    if (!GetFiringDirectionToHitTarget(currentTarget, out Vector3 firingDirection, out hitPosition, out hitTime))
    {
        return false;
    }
    
    Quaternion desiredRotation = Quaternion.LookRotation(invertFiringDirection ? -firingDirection : firingDirection);
    Quaternion currentRotation = transformToMove.rotation;
    float maxDeltaAngle = maxRotationDegreesPerSecond * Time.fixedDeltaTime;

transformToMove2.rotation = Quaternion.RotateTowards(
    currentRotation, 
    desiredRotation, 
    maxDeltaAngle
);
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

It sounds like your desiredRotation variable is being influenced by the current rotation of the player. Your desiredRotation is being set based on the LookRotation() of a firingDirection. Is your GetFiringDirectionToHitTarget() method using the player's current position, by chance?

over 4 years ago · Santiago Trujillo Relatório

0

Basically it was a “stupid” bug. In my manual control, I was applying the rotation to transformToMove and in the Editor it was applied to a child of the parent transform. So obviously there was going to be an offset if the initial rotation is for the parent (transformToMove2).

Sorry about this, I will leave the post up to teach Unity and even programmers in the future. That no matter how many times you look over your code and refactor it, the solution can be right in front of your eyes. For example, using the wrong transform.....

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda