My game is a side scroller, where the player controls a car. You can shoot from this car, in the direction of the mouse. The problem is, that the car at times move really fast, faster than the bullet, so the bullets just kinda fly along side the car.
I’m more asking for general advice on how you would approach this problem, rather than a code fix or a script.
What i have attempted so far:
I started by having this code snippet in my bullet script that makes the bullet move in the desired direction:
transform.position += shootDir *moveSpeed * Time.deltaTime;
My first thought was that i maybe could add the players velocity to the bullet, but the above code snippet doesn’t change the rigidbody velocity, so i had to move it somehow else.
bulletrb.velocity = bulletDir*moveSpeed;
I tried the above approach, so i had a velocity i could work with. the bulletrb is the bullet rigidbody, and the bulletDir is a Vector2 direction.
This makes the bullet fly in a constant speed in the desired direction, and gives me a velocity.
Now i have been trying to figure out if i can add the players velocity to the bullet, which i do with:
bulletrb.velocity = playerrb.velocity;
in the Awake() function.
But since bulletrb.velocity = bulletDir*moveSpeed; also just sets the velocity, i tried adding a +
bulletrb.velocity += bulletDir*moveSpeed;
But now it doesn’t move in a constant speed (Which is the reason i didn’t use AddForce)
and also since i control the direction i’m shooting through the velocity, the added player velocity skeewes the direction of the projectile.
How would you make the projectile move relative to the player?