Reputation: 303
Is it possible to increase the movement speed by the distance/time I walked? At this moment I'm only able to set the speed to a fixed value.
void Start () {
rb = GetComponent<Rigidbody2D>();
}
void Update () {
rb.velocity = new Vector2(3, rb.velocity.y);
if (Input.GetMouseButtonDown(0) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, 3);
}
}
Upvotes: 1
Views: 148
Reputation: 1143
I think what you are asking for it's something like this
float timeWalking = 1.0f;
void Update () {
rb.velocity = new Vector2(3, rb.velocity.y);
if (Input.GetMouseButtonDown(0) && onGround)
{
timeWalking += Time.deltaTime;
rb.velocity = new Vector2(rb.velocity.x, 3) * timeWalking;
}
if (Input.GetMouseButtonUp(0) && onGround)
timeWalking = 1.0f;
}
Upvotes: 1
Reputation: 4788
You probably want to investigate Time.deltaTime
, which gives you the elapsed time since the last frame. With that info, plus knowledge of how far you are currently set to travel each frame, you can probably do whatever you need.
Upvotes: 2