- Home /
Moving an object after player click?
Hello, I have a script that slightly adds velocity which seems to work well but I still have the issue of consistently applying the force so the object keeps moving instead of just sliding forward a little. Any ideas on how to make it keep applying force instead of just pushing it once?
void FixedUpdate()
{
if(Input.GetMouseButtonUp(0))
{
rb.velocity = new Vector2(1, 0);
}
}
Change to Input.Get$$anonymous$$ouseButton since most likely the force isnt big enough for moving the object.
Answer by Hellium · Mar 18, 2019 at 08:39 AM
public float LinearDrag = 1;
private Vector2 velocity;
void Update()
{
if(Input.GetMouseButtonUp(0))
{
velocity = new Vector2(1, 0);
}
else if( velocity.x > Time.deltaTime * LinearDrag )
velocity.x -= Time.deltaTime * LinearDrag;
else
velocity.x = 0;
}
void FixedUpdate()
{
rb.velocity = velocity;
}
That fixed the constant speed problem but it moves automatically even without me clicking now.
Then, keep your current code and decrease the value of linear drag
of the Rigidbody2D and/or increase the velocity.
I've edited my answer to handle the drag. Give it a try.
hello! try removing the Up from the Get$$anonymous$$ouseButtonUp and leave it like Get$$anonymous$$ouseButton
I tweaked the rigidbody settings and it worked! Thanks for the help