2D character controller locked in place (C#)

Hello! I’ve been trying to script a 2D character controller using WASD/arrow keys (I need the character to be able to move in all four directions). I’ve been playing around/looking for various solutions but I can’t seem to get it to work that I want to?

The current script that I have actually moves the character but it’s locked into position - like it’ll move one unit and then automatically return to the starting position. I know what the problem is but I don’t know how to fix it?

public float range;
    	public	void Update (){
    		float h = Input.GetAxisRaw ("Horizontal");
    		float v = Input.GetAxisRaw ("Vertical");
    		float xPos = h;
    		float yPos = v-4 ;
    		transform.position = new Vector3 (xPos, yPos, Time.deltaTime);}

I understand that I need to somehow replace (or add) the character’s position within the Vector3 segment but I don’t know what terms will bring it up.

If this isn’t the best way to make a simple WASD controller, then could somebody suggest a better alternative? I should also note that I would like to keep the choppy movements that getAxisRaw gives me!

Thanks in advance!

Here is a simple code I made to move to the left and to the right:

void FixedUpdate() {
						float move = Input.GetAxis ("Horizontal");
						rigidbody2D.velocity = new Vector2 (move * speed * sensivityovertime, rigidbody2D.velocity.y);

To make it work you have to attach a rigidbody2D to your object. If you want it to jumo (I dont know if you need it) then you can use the AddForce function. Here is all the reference to rigidbody2D:

Algo you may need to configure the inputs or detect wich key is being pressed manually:


detect keys manually → The getkey o getkeydown function should work

Hope this helps!

Edit: Since im using a rigidbody I code the movement inside fixedupdate
reference here. But if you want to use the Update instead and deal with objects without rigidbodies you should make your movement scale with the time passed between frames: here is the reference, you should use the Time.deltatime

I dont know if I posted more information than necessary but I guess all this will help you, good luck!