void Update()
{
if (Input.GetKeyDown(“w”))
{
Fire();
}
}
void Fire()
{
rb.MovePosition(rb.position + Vector2.down * speed * Time.deltaTime);
}
I have the following code and every time I press W, the ball moves only down for 1 instead of multiplying with the speed variable. How can I make the object move as defined in the Fire().
Raimi
July 9, 2017, 5:51pm
2
I think you need to use +=
void Fire()
{
rb.position += Vector2.down * speed * Time.deltaTime;
}
Edit: Sorry Use this instead…
if(Input.GetKey(KeyCode.W))
{
rb.position += Vector2.down * speed * Time.deltaTime;
}
You want to press W once and have the object continue on for forever?
MovePosition is one time kind of thing unless you call it all the time. Since it’s a rigidbody try AddForce instead.