- Home /
I want to make my player move forwads on the direction that its looking on 2d. How do i do that?
Im quite new on the game development, my movement script for a 2d sprite is quite simple, i did when you press q the sprite rotates counterclockways, and when you press e it rotates clockways, but when i move my player it stil moves up down left and right and not in the direction that its rotated. Here is my script. Need help <3
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody2D rb;
private Vector2 moveVelocity;
public float rotateSpeed;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput.normalized * speed;
}
private void FixedUpdate()
{
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
if (Input.GetKey(KeyCode.Q))
{
transform.Rotate(Vector3.forward * rotateSpeed);
}
if (Input.GetKey(KeyCode.E))
{
transform.Rotate(Vector3.back * rotateSpeed);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object move on z axis while giving force on x axis 0 Answers
Issue w/ Difference between Cube and Capsule 0 Answers
How to limit an object's position to a spherical radius? 0 Answers