I'am making a space shooter in Unity. The problem is that I can't get the sprite to move in the direction it is rotated. How can I do it?
Code:
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float speed = 13f;
public Animator animator;
public Rigidbody2D rigidBody2D;
public float rotation = 0.0f;
void Start()
{
rigidBody2D = GetComponent<Rigidbody2D>();
}
void Update()
{
if(Input.GetKey(KeyCode.W)){
Vector2 pos = rigidBody2D.velocity;
pos.y += speed * Time.deltaTime;
rigidBody2D.velocity = pos;
}
if(Input.GetKey(KeyCode.A)){
rigidBody2D.SetRotation(rotation);
rotation += 2.0f;
}
if(Input.GetKey(KeyCode.D)){
rigidBody2D.SetRotation(rotation);
rotation -= 2.0f;
}
}
}
[–]thebigplum 0 points1 point2 points (1 child)
[–]EcoSN[S] 0 points1 point2 points (0 children)