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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Rendering; public class Player : MonoBehaviour { private Rigidbody2D rb; [SerializeField] private float moveXSpeed; [SerializeField] private float moveYSpeed; [SerializeField] private float moveXSpeedMul; [SerializeField] private float moveYSpeedMul; [SerializeField] private float rotateSpeed; [SerializeField] private float rotateOffset; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { Movement(); OnClickMovement(); } void Movement() { moveXSpeed += moveXSpeedMul * Time.deltaTime; Vector2 moveOffset = new Vector2(moveXSpeed, 0); transform.Translate(moveOffset); } void OnClickMovement() { moveYSpeed += moveYSpeedMul * Time.deltaTime; if (Input.GetMouseButton(0)) { rb.velocity = Vector2.up * moveYSpeed; } if (Input.GetMouseButton(1)) { rb.velocity = Vector2.up * -moveYSpeed; } } } Please help me out.
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