Reputation: 25
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);
}
Upvotes: 2
Views: 18705
Reputation: 350
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
Upvotes: 0
Reputation: 435
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;
}
Upvotes: 3