Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

how to add multiple forces on a single object

$
0
0
my player uses rigidbody.velocity to move, and also have an alien that explodes, and also has an explosion effect that pushes objects away, look here what the problem is: https://drive.google.com/open?id=1_-mfqw9JTDO01FMklGxCtup5ZQoGu-4R as you see in the video, all mobs flew away, only the player because i'm moving him using rigidbody, i tried to disable my controller script, and it worked, anyone help pls, here's my movement piece of code: void FixedUpdate() { if (horizontal != 0 && vertical != 0) { rb.velocity = new Vector2((horizontal * speed) * speedLimiter, (vertical * speed) * speedLimiter); } else { rb.velocity = new Vector2(horizontal * speed, vertical * speed); } } and here is the explosion piece of code: public override void FixedUpdate() { base.FixedUpdate(); checkDistance(); } void checkDistance() { if (player != null) { if (Vector2.Distance(transform.position, player.position) <= 1.75f) { Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, radius); foreach(Collider2D collider in colliders) { Rigidbody2D rb = collider.GetComponent(); if(rb != null) { AddExplosionForce(rb, explosionForce, transform.position, radius); } } GameObject instance = SpawnObject.WithRotation(WormSplashEffect, transform); Destroy(instance, 1f); Destroy(gameObject); } } } public void AddExplosionForce(Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius) { var dir = (body.transform.position - explosionPosition); float wearoff = 1 - (dir.magnitude / explosionRadius); body.AddForce(dir.normalized * explosionForce * wearoff); }

Viewing all articles
Browse latest Browse all 1264

Trending Articles