Search Unity

Need help with rotation

Discussion in '2D' started by Kratos1233, Nov 20, 2022.

  1. Kratos1233

    Kratos1233

    Joined:
    May 27, 2021
    Posts:
    3
    I'm working on my first ever game and I'm trying to create mechanics like Geometry Dash Spaceship.
    Like this - https://youtube.com/shorts/AOtjdPyLQuM?feature=share

    I wrote this code for the movement and it's working but i also need to rotate in the direction of movement. I'm trying but I'm unable to do that. Here's my code.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Rendering;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     private Rigidbody2D rb;
    9.  
    10.     [SerializeField] private float moveXSpeed;
    11.     [SerializeField] private float moveYSpeed;
    12.  
    13.     [SerializeField] private float moveXSpeedMul;
    14.     [SerializeField] private float moveYSpeedMul;
    15.  
    16.     [SerializeField] private float rotateSpeed;
    17.     [SerializeField] private float rotateOffset;
    18.  
    19.  
    20.     void Start()
    21.     {
    22.         rb = GetComponent<Rigidbody2D>();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         Movement();
    28.         OnClickMovement();
    29.     }
    30.  
    31.     void Movement()
    32.     {
    33.         moveXSpeed += moveXSpeedMul * Time.deltaTime;
    34.         Vector2 moveOffset = new Vector2(moveXSpeed, 0);
    35.         transform.Translate(moveOffset);
    36.     }
    37.  
    38.     void OnClickMovement()
    39.     {
    40.         moveYSpeed += moveYSpeedMul * Time.deltaTime;
    41.  
    42.         if (Input.GetMouseButton(0))
    43.         {
    44.             rb.velocity = Vector2.up * moveYSpeed;
    45.         }
    46.  
    47.         if (Input.GetMouseButton(1))
    48.         {
    49.             rb.velocity = Vector2.up * -moveYSpeed;
    50.         }
    51.     }
    52. }
    Please help me out.
     
    Last edited: Nov 20, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121

    As for the actual problem, looks like it simply turns to face the direction of current motion, based on the video above. You can use the X/Y movement at any given instant to:

    - compute the angle to face based on X/Y speed. Use
    Mathf.Atan2()
    and
    Mathf.Rad2Deg
    to get the desired headin in degrees

    - use that degrees heading to call the .MoveRotation() method (which takes a float in 2D) to turn your spaceship