Question by
eriknastesjo · Jan 04, 2020 at 04:43 PM ·
spriterigidbody2dshakevibrationshakeposition
How shake an already moving object?
In my game I have rigidbody and sprite as separate children of the player. I'm trying to make the sprite vibrate when low on health while still being able to move it with rigidbody. But while my script (see below) successfully makes the sprite vibrate it also gets stuck in about the same position. Is there any way to make the player both move and vibrate at the same time?
private IEnumerator Shake()
{
while (healthCount == 3)
{
Vector2 currentPlace = new Vector2(transform.position.x, transform.position.y); // need this to prevent object wandering away by itself.
float a = Random.Range(-1, 1) * magnitude1;
float b = Random.Range(-1, 1) * magnitude1;
transform.position = new Vector2(transform.position.x + a, transform.position.y + b);
yield return null;
transform.position = currentPlace;
}
}
Comment
Your answer
Follow this Question
Related Questions
How do i stop my cube sprite from tilting? 1 Answer
Rigid body vibrating? 0 Answers
2D Characters slowly drifts downwards? 1 Answer
Spin a 2d Sprite with touch or mouse 0 Answers
LERPing Rigidbody2D still passing through collider 0 Answers