Search Unity

Question rotate in specific time

Discussion in '2D' started by mahanbst, Oct 18, 2020.

  1. mahanbst

    mahanbst

    Joined:
    Oct 10, 2020
    Posts:
    4
    hi guys I making a game. in my game if u press space u go up or down (we have 2 ground) and I change gravity scale in rigidBody2D in player GameObject.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private Rigidbody2D rigid;
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         rigid = GetComponent<Rigidbody2D>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         // if (Input.touchCount > 0)
    18.         // {
    19.         //     Touch tch = Input.GetTouch(0);
    20.             //if (tch.phase == TouchPhase.Began)
    21.             if(Input.GetKeyDown(KeyCode.Space))
    22.             {
    23.                 if (rigid.gravityScale == 3)
    24.                 {
    25.                     rigid.gravityScale = -3;
    26.                 }
    27.                 else
    28.                 {
    29.                     rigid.gravityScale = 3;
    30.                    
    31.                 }
    32.             }
    33.         //}
    34.     }
    35. }
    36.  
    now i need to rotate player GameObject 180 degree (before hit ground top or bottom)
    please help
     
  2. mahanbst

    mahanbst

    Joined:
    Oct 10, 2020
    Posts:
    4
    I'm so sorry if my English is terrible
     
  3. mahanbst

    mahanbst

    Joined:
    Oct 10, 2020
    Posts:
    4
    Im still waiting
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Rotate the character sprite. Look up how to rotate a sprite. You will probably rotate the Z on the Transform.Rotation.
     
  5. mahanbst

    mahanbst

    Joined:
    Oct 10, 2020
    Posts:
    4
    i dont want rotate instantly
    i want when player press jump character start rotating to when hit ground
     
  6. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Look up a Lerp function, it will slowly change between the 2 values.
    transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), rotationSpeed * Time.deltaTime);
     
  7. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    Maybe you can rotate it gradually with a Coroutine