- Home /
I need Help with AdForce pls :(
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<Rigidbody2D>();
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);
}
Answer by Captain_Pineapple · Aug 11, 2019 at 09:01 PM
Hey there,
first off: please no begging in a posts headline... stick to the important informations...
regarding your problem: It's all in the way how you handle velocity in the FixedUpdate
...
I will assume that horizontal
and vertical
are direct values resulting from inputs? If yes then assume a situation where you apply force. This force will be translated to a change in velocity. Then the FixedUpdate
occures and afterwards your object will be moved according to the velocity it has. But every fixed update you set it's velocity to a fix value that is tied to your input values.
So where to start fixing this? choose another way to apply the input to your character movement or change the calculations in your fixed update to actually include the velocity of the last frame. (which will the automatically also include the explosion force)
Let me know if anything was unclear.
how do i actually do that? - "change the calculations in your fixed update to actually include the velocity of the last frame. (which will the automatically also include the explosion force)", sry, but never done this b4
I put this together:
if (rb.velocity.magnitude <= speedLimiter)
{
rb.velocity = Vector2.Clamp$$anonymous$$agnitude(new Vector2((horizontal * speed), (vertical * speed)), speedLimiter);
}
works in my head but is not tested... idea is to only fiddle with teh velocity when the magnitude is smaller than max speed. So an explosion should automatically take your control away. As soon as you drop below a certain speed you should be able to move with that maxSpeed again.
And Vector2.Clamp$$anonymous$$agnitude will make sure that your total velocity never exceeds the value of speedLimiter.
Just plug that code into your fixedUpdate
and remove your current code there.
Thank you So much man your a genius! :'DDDDDDDD
Your answer
Follow this Question
Related Questions
Jumping with Rigidbody.AddForce not working? 1 Answer
Adding force to rigidbody2d to slide 1 Answer
rigidbody2D.AddForce causing other objects to float away 1 Answer
[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer
How can I tweak acceleration and deceleration of a Rigidbody2D with .AddForce()? 0 Answers