Shooting a cannonball (Projectile) doesn't work - C++

Hello Answerhub,

Situation:

I want to create a “Topdown” ship battle game. My first step was to create a pawn without a mesh and attach a camera to it. So the pawn always moves on top of the terrain and the camera follows this pawn. At the same time I have a second pawn. This pawn is the ship. The player can controll the camera and the ship at the same time, but can also attach (focus) the camera on the ship, so that the camera follows the ship movement.

Problem:

So far so good, everything works. Now I want to shoot cannonballs from the ship. The ship has two sides right (true) and left (false). True and false, because I have created a bool which represents the select side. (Sides can be switch with Q and E: When you press Q the bool is updated to false and in my Shoot function the false branche is executed)

I have a Cannonball.h with:

  1. A UStaticMeshComponent as RootComponent
  2. A USphereComponent
  3. A UProjectileMovementComponent

In my Ship.cpp when the Shoot function is executed I want to spawn a cannonball with physics simulated and a launche imulse.

A cannonball should be spawned at a socket,

than added to a TArray and after this fired.
The Ship::Shoot() fuction looks like this:

And the ACannonball::FireInDirection() function looks like this:

I have tried a lot of different things for the last two days, but nothing solved my problem.

At the moment the cannonball is spawned at the socket (“Cannon_r_1”) and has physics enabled, but just drops down and trough the level. (When I look in the “Details” tab of the spawned cannonball at runtime the velocity is updated, but the ball doesn’t move)

But I have tried different things:

  1. I’ve followed the Projectile Documentaion:
    Implementing Projectiles in Unreal Engine | Unreal Engine 5.1 Documentation but when I do this my actor as no visible Mesh

  2. I’ve tried it without a ProjectileMoveComponent and just with AddImpule/AddForce nothing happend

  3. I’ve set the SphereComponent to RootComponent, but than the actor was spawned at the socket and immediately teleported to ((0, 0, 0)?) or another point. There it just droped again.

  4. I’ve tried different CoollisionSettings nothing happend. And when I don’t check the “Simulate Physics” Box the actor is spawned, but stays in the air.

I’ve tried some more things but nothing changed my mess. Mybe someone understands my problem and can help me. If you need more detailed information or the situation is unclear tell me.

Greetings

Whats the Initial Speed & Velocity? Did you check if its already colliding with something? Bump up the Numbers higher and Spawn it somewhere high above (just to make sure theres no intial collision with something). If you turned on Simulation for both your Collision and Mesh will move independent of each other (Show collision to see if thats the case Screen capture - 125775976e1d741ad4566cfbc6b16450 - Gyazo) turn it on only for the root. Also Check your Mass Settings they have influence over velocity. In Short need more info about your Setup :stuck_out_tongue_winking_eye:

Try again report back =)

I’ve tried some things and looked on Velocity and Initial Speed. The Initial Speed was set to 3000 so this was fine. But the Velocity was set to (1/0/0) and and I want to shoot in Y direction so I’ve changed it to (0/1/0). I’ve also set simulate physics only for the root.

Now at least the cannonball gets an impulse and does SOMETHING. When I shoot, it spawns, gets an impulse but doesn’t fly in Y or -Y it starts to do somersaults and changes its direction every second. The other things is after ~5 shots the moventment behavior changes and it just drops in the wrong Y direction with a minimal speed.

Movement for the first ~5 shots:

Movement after the ~5 shots: