Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to set Y Rotation in 2D

Discussion in 'Scripting' started by FloatyPixel, Dec 30, 2015.

  1. FloatyPixel

    FloatyPixel

    Joined:
    Dec 11, 2015
    Posts:
    7
    Hello

    I was working on AI, and this part makes random crazy rotation for enemy and it's invisible.
    Code (CSharp):
    1. Collider2D followPlayer = Physics2D.OverlapCircle (transform.position, attackRange, LayerMask.NameToLayer ("player"));
    2. transform.LookAt(target.position);
    3. transform.Translate(moveSpeed * Vector3.forward * Time.deltaTime);
    RotationNumbers.png
    If I pause the game and set rotation to 0, 0, 0 enemy is visible. Only Y is important.
    Rotation of my player is at 0, 0, 0.

    So how can I set rotation to 0 in script for 2D?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You usually only rotate around Z when dealing with rotations in unity 2D. Use Mathf.Atan2 to find the z rotation, given a direction vector.
     
  3. FloatyPixel

    FloatyPixel

    Joined:
    Dec 11, 2015
    Posts:
    7
    I put transform.eulerAngles = new Vector3 (0, 0, 0); in all places in the code ( Hopefully that won't be a problem).
    Thanks anyway ;)
    But now enemies stack on each other when they follow me :(