Can't get tranform.Rotate to roate the object?

I want to flip 2d sprite without using code to change its scale because it has a problem with child object that attach to it, Rotate 180 degree work but in scripting. It won’t work. I don’t know which component use t rotate object at all as I never use rotate code at all. Here my code:

if(deltaX > 0){
            //transform.localScale = new Vector3 (1, 1, 1);
        }else if(deltaX < 0){
            //transform.localScale = new Vector3 (-1, 1, 1);
            //transform.localEulerAngles = new Vector3 (0f, 180f, 0f);
              transform.Rotate(0,180,0);
        }

Do you just want to set the rotation immediately? If so, you can use this.

transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);

I’ve tried that. It didn’t work. There’s nothing work beside the localScale or Flip Sprite from the Sprite render component, but it has problem with my child property that has different setup.
What is the actual rotate code for Unity? There’s so many rotate code but non of them work for me.

I use this command to make an object rotate over time
transform.Rotate(Vector3.right * Time.deltaTime);

You need to put a debug.Log(“Trying to Rotate”); in your code to verify that it’s even being called

I’m working on 2D project. I didn’t want the object to rotate overtime but instant rotate. The transform.Rotate and other roate code doesn’t work. What is wrong with these code?

All we know is small peace of the code. So it’s hard for us to tell you what’s wrong.
So please add the debug.Log(); or print(); to help you trouble shoot. please verify that the code is being executed.

if(deltaX > 0)
{
print("deltaX is > 0 = " + deltaX);
//transform.localScale = new Vector3 (1, 1, 1);
}else if(deltaX < 0)
{
print("deltaX is < 0 = " + deltaX);
//transform.localScale = new Vector3 (-1, 1, 1);
//transform.localEulerAngles = new Vector3 (0f, 180f, 0f);
transform.rotation = Quaternoin.Euler(0.0f, 180.0.f, 0.0f);
}

It sounds like you’re rotating the wrong transform. Without more code and/or the gameobject hierarchy no one will be able to help you.

The deltaX is from the Input.GetAxis (“Horizontal”) which mean deltaX > 0 facing right and deltaX < 0 facing left.
I’ve debug it. It’s work fine with localScale but it won’t work with any rotate code from transform component.

public class Player : MonoBehaviour {

    private Rigidbody2D _myRigidbody;
    private Animator _myAnimator;
    private SpriteRenderer _mySprite;

    private float distanceToGround = 0;
    public float Speed = 9.0f;
    public float LimitY = 0;
    public float LimitOffset = 0;
    public float jumpSpeed = 9.0f;
    private float keyDownTime = 0;
    public float keyDownCD = 0.50f;

    bool jumpY = false;
    public float deltaX = 0;   

    public bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.1f;
    public LayerMask WhatIsGround;

    bool Attack;

    void Start()
    {
        _mySprite = GetComponent <SpriteRenderer> ();
        _myRigidbody = GetComponent <Rigidbody2D> ();
        _myAnimator = GetComponent <Animator> ();
    }
       
    void Update () {
        float clampZ = Mathf.Clamp (0, 0, 0);
        transform.localEulerAngles = new Vector3 (0, 0, clampZ);
        DelayJumpKey ();
        LimitY = transform.position.y;
        LimitOffset = LimitY - LimitY;
    }

    void FixedUpdate() {
        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, WhatIsGround);

        Movement ();

        MyAnimator (deltaX);

        FlipCharacter ();

    }

    void Movement(){
        deltaX = Input.GetAxis ("Horizontal") * Speed;
        Vector2 moveHor = new Vector2 (deltaX, 0);
        if(jumpY){
            moveHor.y = jumpSpeed;
            _myRigidbody.velocity = new Vector2 (deltaX, jumpSpeed);
        }else
             _myRigidbody.velocity = new Vector2 (deltaX, _myRigidbody.velocity.y);
    }

    void MyAnimator(float deltaX){
        _myAnimator.SetFloat ("speed", Mathf.Abs (deltaX));
        _myAnimator.SetBool ("Ground", grounded);
    }

    void FlipCharacter(){
        Vector3 flip = transform.localScale;
        if(deltaX > 0){
            transform.localScale = new Vector3 (1, 1, 1);
        }else if(deltaX < 0){
            transform.localScale = new Vector3 (-1, 1, 1);
        }
    }

    void DelayJumpKey (){
        if (Input.GetKeyDown ("k"))
            keyDownTime = keyDownCD;   
        if (Input.GetKey ("k") && keyDownTime > 0) {
            keyDownTime -= Time.deltaTime;
            jumpY = true;
        } else
            jumpY = false;
    }
       
}

You’re resetting your rotation every update… Please remove line 34 and put your FlipCharacter method call in Update;

if (deltaX > 0)
{
    transform.rotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
}
else if (deltaX < 0)
{
    transform.rotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
}

yep, I think Jim is correct.

It was a debug code I forgot to remove. I was so frustrated and thinking that there is a proper rotation code.

Thank for helping me out. I think that I’ve inspect everything. I still make the same mistake again & again as a programmer. Always think outside of the box!