UPDATE: I solved and finished the game a while ago, my problem was that I was doing the explosions all wrong, my player didn’t have the velocity I thought it did. It was being pushed by the expanding collider of my explosion. I made the explosion Collider onsize and that fixed my issue. This was all due to a lack of knowledge of how unity physics work.
Hey I’m making a 2D space shooter, I have my player moving friction-less like I want and collisions with asteroids work, but when the player collides with an explosion the player slows down which is not what I want, the explosion is a circle that expands as the circle expands it pushes the player away, but when the explosion is destroyed the player no longer being pushed slows down, I want the player to keep it’s velocity, but when the explosion is expanding and pushing the player the player’s velocity does not seem to increase. I am using a physics material that is friction-less and I have tried using add force in OnCollisionEnter and Exit, but it does not seem to work right, I hope someone has a good answer
here is my explosion code:
public class Explosion : MonoBehaviour
{
CircleCollider2D cc;
void Start()
{
cc = GetComponent<CircleCollider2D>();
}
void Update()
{
if (GameManager.gameState != GameManager.GameStates.play)
return;
//increase the collider
cc.radius += Time.deltaTime/2;
//destroy object after 1.5 seconds
Destroy(gameObject, 1.5f);
}
}
Update: Sorry I didn’t post my player code because there is nothing in it that slows down the player, also it happens with the asteroids when they hit the explosion as well, the increasing size of the collider is not causing the problem I just tried it with a fixed sized circle, the player and asteroid collisions work fine, it seems as the explosion collider expands it pushes the other objects away but does not effect other object’s velocity for some reason…
Update 2:
Here is the code I spawn the explosion with:
public class LaserBeam : MonoBehaviour
{
float speed = GameManager.LIGHT_SPEED;
public Explosion explosion;
void Start()
{
this.GetComponent<Rigidbody2D>().AddForce(transform.up * speed);
}
void Update()
{
if (GameManager.gameState != GameManager.GameStates.play)
return;
//destroy object after 1 second
Destroy(gameObject, 1);
}
void OnCollisionEnter2D(Collision2D col)
{
if (GameManager.gameState != GameManager.GameStates.play)
return;
//destroy target
if (col.gameObject.tag == "Asteroid") //if (col.gameObject.tag != "Player")
{
Vector2 size = col.transform.localScale*4;
Destroy(col.gameObject);
//create explosion
explosion = Instantiate<Explosion>(explosion);
explosion.transform.position = col.transform.position;
explosion.transform.localScale = size;
//destroy object
Destroy(gameObject);
GameManager.playerScore += 50;
}
}
}
also my objects all have a 2d physics material with 0 friction and .333 bounciness
Here is the ridgidbody for my player: