I have ship and instantiated bullets by poolManager, my ship is able to rotate and
fly around freely, and shoot bullets of course. Everything in Top-Down perspective
The Bullet have components:
-
Kinematic RigidBody2D
-
Capsule Trigger Collider 2D
When player stays still and shoots, bullets speed looks fine, but moving forward and shooting makes bullets head backward due to ship speed, also rotating, flying and shooting at same time makes it very odd, bullets don’t move straight I think it’s also due to speed difference.
My player ship’s components:
-
Dynamic RigidBody2D
-
Box Collider 2D
I was trying to add
player.body.velocity
To bullet speed Translate, but the final bullet speed, it was way too fast.
What i want are plasma projectiles flying with medium speed.
Here is my Bullet Class
public class Bullet : Pool_Object {
public float bulletSpeed = 5;
public Vector2 ownerVelocity;
public int duration = 2;
private float counter;
private Player owner;
private void Awake()
{
owner = FindObjectOfType();
ownerVelocity = new Vector2(0, 0);
}
private void OnEnable()
{
ownerVelocity=owner.Body.velocity;
if (owner != null)
{
Debug.Log("Player Velocity: "+ownerVelocity);
}
}
void Update ()
{
if (gameObject.activeInHierarchy)
{
transform.Translate(Vector2.up * bulletSpeed * Time.deltaTime +> ownerVelocity);
counter += Time.deltaTime;
}
if (duration <= counter )
{
gameObject.SetActive(false);
counter = 0;
}
}
In this case i actually looking for a way to make bullets move proportionally to the ship movement, cause they’re too fast, no matter what i do they just blinking thru the view field.
UPDATE TO ABOVE CLASS:
private void OnEnable()
{
ownerVelocity = ownerBody.velocity.magnitude;
}
void Update ()
{
if (gameObject.activeInHierarchy)
{
transform.Translate(Vector3.up* Time.deltaTime* (ownerVelocity+bulletSpeed));
counter += Time.deltaTime;
}
if (duration <= counter )
{
gameObject.SetActive(false);
counter = 0;
}
}
It does look way better now, but still isn’t a solution
I already tried:
-
Normalizing ownerVelocity;
-
Scalling down bulletSpeed;
-
Using RigidBody.AddForce with dynamic RigidBody2D;
-
Assing speed directly by bullet.velocity += owner.velocity;
-
Using transform.up instead of Vector2.up;
-
Using space.world;
-
Dividing owner.velocity;
Tried Answers:
-
Using transform.forward instead of Vector2.up
-
Removed bullet instances from player parentship
Reuse bullets at muzzle of weapon:
public class Barrel : MonoBehaviour
{
Transform firePoint;
private GameObject bulletType;
void Awake ()
{
bulletType = GetComponentInParent<Weapon>().bulletType;
firePoint = transform.GetChild(0);
if (firePoint == null)
{
Debug.LogError("No FirePoint? WHAT?!");
}
}
public void Shoot()
{
Pool_Manager.Instance.ReuseObject(bulletType, firePoint.position,firePoint.rotation);
}
Some methods that actually are setting my bullets to positions, rotations etc
public ObjectInstance(GameObject objectInstance)
{
gameObject = objectInstance;
transform = gameObject.transform;
gameObject.SetActive(false);
if (gameObject.GetComponent<Pool_Object>())
{
hasPoolObjectComponent = true;
poolObjectScript = gameObject.GetComponent<Pool_Object>();
}
}
public void Reuse(Vector3 position, Quaternion rotation)
{
gameObject.SetActive(true);
transform.position = position;
transform.rotation = rotation;
if (hasPoolObjectComponent)
{
poolObjectScript.OnObjectReuse();
}
}