Rotating an object on Z axis while moving


Hello,
I really don’t know what should I do… I have no more ideas :confused: I’ve already seen some q&a about rotating but “I still haven’t found what I’m looking for:smiley:
I’m creating 2D game and now, I’m making object rotations. I think my English is bad so I add a picture for you with that, what I’m thinking about.

Object has to rotate only on Z axis while its moving up or down or sides.

I wrote that, but an object is just making a really silly things when I move - I know that :smiley: .

    public float speed;
    void Update () {
        		float movelr = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
        transform.Translate (movelr, 0, 0); 
        
        		float moveud = Input.GetAxis("Vertical") * speed * Time.deltaTime;
        		
        		transform.Translate (0, moveud, 0);
    
        		if(Input.GetKey(KeyCode.C))
        		{	
        		transform.Rotate(0,0, Time.deltaTime * 180);
        		}
//and I have gravity, when object is falling I have to rotate him on Z axis, I hope you understand me :/
			if (transform.position.y > -4.5f) 
			{
			Physics.gravity = new Vector3 (0, -4.5f, 0);
			}
    }

I start with unity and C#, please guys, help me :slight_smile:
Thanks

The Rotate function by you is selected absolutely correctly. But it has some distinctions in case of parameter passing. That your object rotated only round an axis z (as far as I understood, it is its coordinate axes are taken as a basis), simply write so:

 transform.Rotate(Vector3.forward, Time.deltaTime * 180, Space.self)

Here, Vector3.forward says that you turn on an axis z, Space.self - use of coordinate axes of turned object. If it is necessary to you that object was turned round a world axis z, simply replace Space.self with Space.world.