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

174
Visualizações
Rotation off by 90 degrees unity

I am trying to rotate my 2d object around my mouse. This is the code I have:

void Update()
{
    Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    Vector3 direction = mousePosition - transform.position;
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.Euler(0, 0, angle);
    // Debug.Log(angle);
}

My object, which is an arrow, is by default pointing down, so before I start the script I set its z rotation to 90, so it faces right. If I delete the transform.rotation line, the angle shown will be right, when my cursor is above it says 90, in the left it says 180 etc. So my question is: Why do I need to add 90 degrees to angle to make this actually work? Why doesn't this version work?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Mathf.Atan2(Also see Wikipedia - Atan2)

Return value is the angle between the x-axis [=Vector3.right] and a 2D vector starting at zero and terminating at (x,y).

It would work as expected if your arrow by default would point to the right but yours is

by default pointing down


you could simply add the offset rotation on top like

transform.rotation = Quaternion.Euler(0, 0, angle + 90);

Alternatively if you need this for multiple objects with different offsets either use a configurable field like

[SerializeField] private float angleOffset;

...

transform.rotation = Quaternion.Euler(0, 0, angle + angleOffset);

Or you could rotate it manually to face correctly to the right before starting the app and store that default offset rotation like

private Quaternion defaultRotation;

private void Awake ()
{
    defaultRotation = transform.rotation;
}

and then do

transform.rotation = defaultRotation * Quaternion.Euler(0, 0, angle);
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