Trying to create a script that will bounce my bouncy character toward a target point.
The idea is to allow the character to 'bounce' on every collision towards the point until they have either reached it or passed it. This is what I have for now.
Any suggestions on how to handle this/ a better system are much appreciated!
Vector2 target;
void OnCollisionEnter2D()
{
if (stateMachine.State == SM.States.moving)
{
if (true)
{
MoveToward(target);
}
else
{
stateMachine.State = SM.States.idle;
}
}
}
public void GoTo(Vector2 destination)
{
target = destination;
if (rigidbody2D.velocity == Vector2.zero)
{
MoveToward(destination);
}
}
void MoveToward(Vector2 destination)
{
stateMachine.State = SM.States.moving;
rigidbody2D.velocity = Vector2.zero;
if (Vector2.Distance(Position2D(),target) > 1.0f)
{
rigidbody2D.AddForce((new Vector2(destination.x, destination.y * 2.0f) - Position2D()) * stats.speed);
}
}
↧