- Home /
Character moves on its own
I am new to Unity and programming. While I think I'm getting pretty good at programming, I still can't code basic things without a tutorial. I have been making a 2D top-down shooter like Enter the Gungeon and I've used Brackey's top-down shooter tutorial to implement character movement. While it has worked reliably on my other projects, I've encountered a bug that hasn't happened to me before. The character's x and y values increase slightly even without input, causing them to move toward the top-right corner. I'm not sure how to fix it, but I have determined that it is because of the code. I think it might have something to do with the new input system, as that is the only difference from the last few times I used it.
[SerializeField] private float moveSpeed = 15f;
[SerializeField] private Rigidbody2D rb;
Vector2 movement;
public InputAction Controls;
private void OnEnable()
{
Controls.Enable();
}
private void OnDisable()
{
Controls.Disable();
}
// Update is called once per frame
void Update()
{
movement = Controls.ReadValue<Vector2>();
}
private void FixedUpdate()
{
rb.velocity = new Vector2(movement.x * moveSpeed + Time.deltaTime, movement.y * moveSpeed + Time.deltaTime);
}
}
Thank you for your help!
Play some rounds in training mode and see if it happens. It’s possible you have two buttons programmed to the same action.
Answer by LPCurse · May 09 at 04:38 AM
I found a solution now. While it isn't perfect, it at least causes the player to be stationary. I found out that all I really had to do was multiply Time.deltaTime instead of adding it. This causes the character to move extremely slowly, but they don't move on their own. It's not perfect, but increasing walking speed to a really high number is better than watching the character slowly drift out of view.
Here's what it looked like before:
rb.velocity = new Vector2(movement.x x moveSpeed + Time.deltaTime, movement.y x moveSpeed + Time.deltaTime);
Here's what it looks like now: rb.velocity = new Vector2(movement.x x moveSpeed x Time.deltaTime, movement.y x moveSpeed x Time.deltaTime);
Change the x to a star, I didn't know they italicize.
Your answer
Follow this Question
Related Questions
Rigidbody causes ragdoll character to fly 4 Answers
Strange Character Controller Behavior Caused by Simulated Gravity and Ground Check 0 Answers
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
2D How to make character slowly stop after moving 1 Answer
2D Platformer physics rotation Movement script - How to roll/flip a square on his edges ? 0 Answers