- Home /
My 2D Player is slightly being dragged sometimes after firing.
I am making an Astroids-like game that has the player as a ship that can move around and turn around freely. There is a small problem I'm experiencing with the movement that, when no movement buttons are being pressed, sometimes makes the player slightly drag off to the bottom-left of where it is pointing at. My code for the movement and mouse aiming is shown here:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMoveScript : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Camera cam;
Vector2 movement;
Vector2 mousePos;
// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f - 180f;
rb.rotation = angle;
}
}
What can be the reason for this?
Answer by Laza_51 · Aug 14, 2020 at 04:07 PM
Try setting the velocity of the player 0 on an axis when the Input on the responding axis is 0.
Just one question, where can I learn how to do that? Currently, I'm trying to find a way to check if the wasd or arrow keys are being pressed, but I can't figure it out.
Your answer
Follow this Question
Related Questions
Can someone explain why my Raycasting doesn't work? please :) 2 Answers
Issue with Animations using WASD Movement in Isometric view 0 Answers
How can I create a sphere rotate in based on the direction it's moving? 1 Answer
How do I create WASD controls for a fps? 3 Answers
Why is my character still falling when the character velocity.y is 0? (2D) 2 Answers