Hello need a tad bit of help with movement

My character keeps sliding around like its a ship in Star Citizen I’d like to either use that like that and make it stop with space or something or get it too be snappy though I can’t seem to figure out what it could be.

my code atm

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerscript : MonoBehaviour {

    public float movementspeed, movementspeedmodifier, currenthealth, maxhealth = 100, currentarmor = 0, maxarmor = 100;
    public bool moving = false;

    public float damage = 10f;
    public float range = 100f;


    private Rigidbody2D rb2d;
    private CharacterController charcont;

    // Use this for initialization
    void Start ()
    {
        rb2d = GetComponent<Rigidbody2D> ();
        charcont = GetComponent<CharacterController>();

        currenthealth = maxhealth;
        maxarmor = currentarmor;

    }
 
    // Update is called once per frame
    void Update ()
    {
        facemouse();
    }

    //deals with rigidbodys only so movement basically
    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.W))
        {
            rb2d.AddForce(Vector2.up * movementspeed * movementspeedmodifier);
            moving = true;
        }

        if (Input.GetKey(KeyCode.S))
        {
            rb2d.AddForce(Vector2.down * movementspeed * movementspeedmodifier);
            moving = true;
        }

        if (Input.GetKey(KeyCode.A))
        {
            rb2d.AddForce(Vector2.left * movementspeed * movementspeedmodifier);
            moving = true;
        }

        if (Input.GetKey(KeyCode.D))
        {
            rb2d.AddForce(Vector2.right * movementspeed * movementspeedmodifier);
            moving = true;
        } 

        if (Input.GetKey(KeyCode.W) != true && Input.GetKey(KeyCode.S) != true && Input.GetKey(KeyCode.A) != true && Input.GetKey(KeyCode.D) != true)
        {
            moving = false;
        }

        //if (Input.GetKey(KeyCode.W))
           // moveinput= new Vector2(Input.GetAxisRaw("Horizontal")), 0f, Input.GetAxisRaw("Vertical"));
            //movevelocity = moveinput * movementspeed;

    }

    void facemouse()
    {
        Vector3 mousePosition = Input.mousePosition;
        mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);

        Vector2 direction = new Vector2(
            mousePosition.x - transform.position.x,
            mousePosition.y - transform.position.y
            );
        transform.up = direction;
    }
}

I think it might be that its using the moving = true thing but isn’t actually doing anything but beats me.
This is for a topdown shooter

Please look at this page (and edit your post): Using code tags properly
It shows you how you can insert code into the forums, so that it looks well formatted and is easier to read.

Could you please clarify your question? Do you want the character to stop when you press spacebar? Or something is happening you’d like to change? I couldn’t quite understand…

So two things to clarify
I was looking to either make it so that when any of the movement buttons are not being pressed then the character would stop, as in it wouldn’t move around anymore. At the moment the character slides around. As if in zero gravity.
That’s where my next though came. I could make it so that I could use the spacebar as a brake of sorts to allow stopping. But that would also require knowing how to make it so that the character doesn’t move around already

-Make it so the character doesn’t slide around when buttons for movement are not being pressed
or
-Make it so that the spacebar can be used as a sort of brake

Sure, well now that makes more sense to me. It’s good that you edited your post. I came here a few minutes ago and saw another paste of your code without tags, and no explanation so I never responded lol.

Okay, so I would try removing (or commenting out) the last line in your movement stuff (in FixedUpdate).
I would try adding “moving = false” at the top of FixedUpdate, and then at the bottom:

if(!moving) rb2d.velocity = Vector2.zero;

If you’re satisfied with that & would like to further discuss the spacebar to stop, we can do that. :slight_smile:

OK. Sorry for the long response time the holidays got in the way of that but I’m back. For the most part I’m happy with it thank you! and I would like to try a mix of the two as well as getting to the shooting aspect of this! If we could.

What I was thinking with the code you just suggested was doing something around

if (Input.GetKey(KeyCode.Space))
        {
            (!moving) rb2d.velocity = Vector2.zero;
        }

But it wont work like that.

Then with the shooting atm I have it just looking at where the mouse is.
Now there are two routes that I know of to do this.

-One being that I have a “bullet” sitting on the layer below the player sitting there as a child which then duplicates with velocity/speed going towards where the mouse is then despawning after hitting something or going a certain distance.

-Raycasting infront of the player to where the mouse is then displaying the “bullet” traveling to it then hitting it.

What is wrong when you try the space bar to stop as you wrote it?

As for your shooting question, I can’t quite follow what you’re asking. Both ideas seem reasonable in words.

Perhaps you could create a thread describing what you want and what you’ve tried , etc… Then, me or someone else could offer some feedback.

If you have more info on the space bar issue here, please follow up. The way I imagine the code you wrote would work is if you’re no longer moving, instead of just stopping for sure with the first code I suggest, you’d have to have that situation plus be holding down the spacebar? :slight_smile: