Hi guys,
I’m currently prototyping a “top down car shooter type thing”. I’m really stuck on implementing shooting with physic based projectiles while the player is moving.
I know that I have to add the players velocity to the projectiles velocity. I managed to do that, but theres still some problem with the initial position of the projectiles. It seems that the projectiles velocity isn’t adjusted fast enough and the initial position of the projectiles looks off.
This is what it looks right now:
This is the script for shooting:
public void Shoot(Vector2 _currentVel)
{
currentFlashDuration = muzzleFlashDuration;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.Euler(0, 0, angle);
GameObject projectileInScene = Instantiate(projectilePrefab, origin, rotation) as GameObject;
projectileInScene.GetComponent<Rigidbody2D>().velocity = _currentVel;
projectileInScene.GetComponent<Rigidbody2D>().AddForce(projectileInScene.transform.right * shotSpeed);
}
I also tried different solution I found around the web like this one:
public void Shoot(Vector2 _currentVel)
{
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.Euler(0, 0, angle);
GameObject projectile = Instantiate(projectilePrefab, origin, rotation) as GameObject;
Vector2 v = projectile.GetComponent<Rigidbody2D>().GetRelativeVector(Vector2.right * shotSpeed);
v += _currentVel;
projectile.GetComponent<Rigidbody2D>().velocity = v;
}
This is giving me the exact same behaviour as the code above.
It drives me crazy that I cant get this straight. This seems so basic. The only other solution right now would be to totally skip the whole physic based projectiles stuff and just use a raycast system, wich would suck.
So if anyone can get me some hints on how to do this, I’de be really thankfull.
Thank you