1
votes

Hi everyone Newbie here.

Top-down Zelda style game.

I'm trying to figure out how to make my player build speed to max speed then reduce speed to stoping.

I already have movement with GetRawAxis but my char moves at max speed the moment I press move with this method.

private void PlayerMovement()
{
    var playerMovement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * moveSpeed;

    transform.position = new Vector3(
        transform.position.x + playerMovement.x,
        transform.position.y + playerMovement.y,
        transform.position.z);
}
2
Please provide Code and/or more info - Milos Romanic
Instead of setting velocity, you could try using the RigidBody's AddForce in the direction of the stick, and add some drag to cause him to stop slowly if no force is being actively applied - Ahndwoo
Here's my current movement method private void PlayerMovement() { var playerMovement = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized * Time.deltaTime * moveSpeed; transform.position = new Vector3( transform.position.x + playerMovement.x, transform.position.y + playerMovement.y, transform.position.z); } - Tas7e
Sorry Don't know how to make the code look properly - Tas7e
@Tas7e Hey I saw the question you just deleted here: stackoverflow.com/questions/67320573/… I was about to post an answer so here it is anyway hope it helps pastebin.com/6ZRed1eu - deKajoo

2 Answers

0
votes

You could try Vector3.SmoothDamp.

This uses a Vector storing the current "speed" and dumps it slowly.

Example can be found here: https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

1
votes

Here is a scenario where you can move your object in the x axis, gradually increasing the speed. You can do the same with the slowing down. Gradually decrease the speed by a value.

float acceleration = 0.6;
float maxSpeed = 30;
float speed = 0;

void Update(){
   if(speed < maxSpeed){
      speed += acceleration * Time.deltaTime;
    }

   transform.position.x = transform.position.x + speed*Time.deltaTime;

}