In unity 2D c# how to rotate an object like geometry dash?

I’m newbie here, but will try to describe as clear as I can. At my game my main character is cube and if press space its jumping dependig on how much you press the button but i want to rotate my cube 90 degrees when pressing the space button but im working about this for 10 hours but i couldn’t achieve how to do that can anyone help me. Thank you

[Game Video][1]

This is my code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerEverything : MonoBehaviour{

    public Transform GroundCheck; // Put the prefab of the ground here
    
    public LayerMask groundLayerBlue; // Insert the layer here.
    public LayerMask groundLayerPurple;
    public bool isGroundedBlue;   
    public bool isGroundedPurple;
    private Rigidbody2D rb2D;
    private float jumpTimer;
    private float timeCount = 0.0f;
    private bool isGrounded;
    public Transform groundCheck;
    public LayerMask groundLayer;

    public float time;

    
    

    // Start is called before the first frame update
    public void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
        

    }

    // Update is called once per frame
    public void Update()
    {
        
        isGroundedBlue = Physics2D.OverlapCircle(GroundCheck.position, 0.15f, groundLayerBlue);
        if (isGroundedBlue)
        {
            transform.position += Vector3.right/60;
        }
        
    
        isGroundedPurple = Physics2D.OverlapCircle(GroundCheck.position, 0.15f, groundLayerPurple);
        if (isGroundedPurple)
        {
            transform.position += Vector3.left / 60;
        }

        if (isGrounded)
        {


            if (Input.GetKey(KeyCode.Space))
            {
                jumpTimer += Time.deltaTime;
               
               
            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                DoJump(350f * jumpTimer);
                jumpTimer = 0;

            }

            
        }
        if (Input.GetKeyUp(KeyCode.Space))
        {
            StartCoroutine(Rotate());

        }


    }
    private void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    }


    public void DoJump(float JumpForce)
    {
        float jumpForceMagnitutde = Mathf.Clamp(JumpForce/3, 0, 50);
        rb2D.AddForce(Vector2.up * jumpForceMagnitutde/3, ForceMode2D.Impulse);
        rb2D.AddForce(Vector2.right*jumpForceMagnitutde/10,ForceMode2D.Impulse);

    }
    IEnumerator Rotate()
    {
        Quaternion startRotation = transform.rotation;
        

        transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, 90), 1);
        yield return null;
    }

}

1 Answer

1

didnt read ur code but rotatetowards and rotatearound should help. check for jump input, then select direction and rotate Z - or + 90 in the direction u want.