Question by
cejche · Dec 09, 2020 at 11:19 PM ·
2d-platformercoroutinelogicshakeshakeposition
(2D) Make a GameObject Shake
I have the following Game Object (box) falling when the player enters a danger zone (triggered by a collider), but what I want is the Box to shake for half a second before falling. I assume a coroutine would be the best way for this to happen and I have the code to make the box fall, but I'm not sure how to make the box shake through my script.
Here's my code, in case it might be usefull to understand
public class FallingBox : MonoBehaviour
{
[SerializeField] float fallSpeed = 3f;
[SerializeField] Transform target;
Rigidbody2D rigidbody;
BoxCollider2D boxCollider;
bool isFalling = false;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
boxCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
var frameMovement = fallSpeed * Time.deltaTime;
var targetPosition = target.transform.position;
if (isFalling)
{
transform.position = Vector2.MoveTowards(transform.position, targetPosition, frameMovement);
if (transform.position == targetPosition)
{
gameObject.layer = LayerMask.NameToLayer("Ground");
boxCollider.enabled = true;
}
}
}
//Called when entering the "danger zone"
public void ChangeStatus()
{
isFalling = true;
}
screenshot-at-dec-09-5-15-16-pm.png
(17.8 kB)
Comment