- Home /
,Top Down Shooter Acceleration and Deceleration/velocity Movement
I am new to unity and new to working with C# and am making a top down shooter. I have gotten basic movement working using WASD or arrow keys for movement control, and my mouse follows my custom cursor fine. The movement I currently have feels very stiff, when I press a button, I jerk in one direction and continue until I let up on the button and jerk to a stop. I am looking to make the control fluid, so if you travel in any direction and change direction, your character decelerates in the direction they were previously traveling and slowly accelerate in the new desired direction. If you let up on all controls, you will drift slowly to a stop. I am trying to work with acceleration, deceleration and velocity and have tried looking up videos on the topic for this specific application. I found a bunch of videos and forum discussions on controlling acceleration and deceleration for platformer running control, which I have tried applying to x and y axis equally, but this doesn't work. I do understand that I need a max speed, acceleration, and deceleration, and velocity is involved, I'm just unsure of how to apply this to my current code and would like to help others facing this problem.
*note: I am currently using rb.addforce to simulate the acceleration I am desiring, but need to change this. I'm sure I need to do something with rb.velocity but not sure on what to do
Below I have attached my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movespeed = 5.0f;
public Rigidbody2D rb;
public Camera cam;
private Vector2 direction;
Vector2 mousePos;
//update is called once per frame
void Update()
{
direction = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
}
void FixedUpdate()
{
direction.Normalize();
rb.AddForce(movespeed * direction);
Vector2 lookDir = mousePos - rb.position;
float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
rb.rotation = angle;
}
}
Answer by knobblez · Jan 22 at 10:51 AM
You may want to play with your Drag for slowing down smoothly. https://docs.unity3d.com/ScriptReference/Rigidbody-drag.html
Generally, you don't want to change velocity(as stated in the documentation) and instead use it as a read-only. I think changing the velocity directly would actually make your movements more sudden. Let AddForce do that work.
I believe what you're looking for is ForceMode:
"m_Rigidbody.AddForce(m_NewForce, ForceMode.Acceleration);"
Answer by CapnFishboy · Jan 22 at 08:27 PM
@knobblez I am making a 2d game, so I am unsure of how to use unity 3d variables for a 2d game. Are you talking about the linear or angular drag on my Rigidbody? again, I'm very new to C#, adding variables and organizing my code, so where are you suggesting I add your suggested line of code in my code? in update or fixed update? Thank you for the help!