C++ Camera Controlled Turrets

That sounds awesome and I am highly interested in how that would be implemented.

I’m actually going to type up a quick little tutorial for it this week. Might get around to it tonight. I like the idea of giving different options for pawn movement for a ship and I’m more then happy to share :slight_smile:

I found something different between Etition 4.12 and Edition 4.11, just as the code shows below:
SpringArm->SetupAttachment(RootComponent); //4.12
SpringArm->AttachTo(RootComponent); //4.11
Maybe this is useful for you.

Excuse me, would you like to share your project with me? I still cannot use the code and when I compile the project, a lot of errors appeared. Sorry to trouble you again.

SetupAttachment or AttachToComponenet are two proper ways of attaching components to a parent. I made the

I would love to help, but the code I have right now has plenty of experimental changes and modifications to it that aren’t ready to be released to the public yet. Can you spoiler me the results of the Output when you compile and I’ll see if I can help. Not the error list, the output (error list doesn’t give accurate readings a lot of the time).

Tahnk you, . When I use the code in TurretBase.h of line37:
// Spawns Munitions
UPROPERTY(Category = BlueprintSpawn, EditDefaultsOnly, BlueprintReadOnly)
TSubclassOf<AMunitionsBase> MunitionsBP;
My VS2015 alway says:
1> F:/Unreal Projects/Gun/Source/Gun/TurretBase.h(39) : Class ‘AMunitionsBase’ not found.

It’s mentioned in the first lines of the turret firing part of the tutorial.
You probably did not create the c++ class nor the munitionsbp.

is on point with his response.

Typically when you get that error you either have:

  1. Missing header files in the include
  2. Missing class (hadn’t created the class itself)

I don’t go into creating the MunitionsBase class as it’s very similar to how you set up the other base classes and is part of the challenge of the tutorial. Its also to help apply what you learned in setting up the ShipBase and TurretBase classes. Everything is in the tutorial you need to make the MunitionsBase class. For the functionality of the projectile, you simply extend it like the turret/ship class. Put the code in to spawn the projectile and apply force to it, then add the projectile to the collision channel. Also to spawn the projectile in the correct sockets, you can mimic the spawning logic of the turrets.

That’s the problem! Thank you!

[QUOTE=
is on point with his response.

Typically when you get that error you either have:

  1. Missing header files in the include
  2. Missing class (hadn’t created the class itself)

I don’t go into creating the MunitionsBase class as it’s very similar to how you set up the other base classes and is part of the challenge of the tutorial. Its also to help apply what you learned in setting up the ShipBase and TurretBase classes. Everything is in the tutorial you need to make the MunitionsBase class. For the functionality of the projectile, you simply extend it like the turret/ship class. Put the code in to spawn the projectile and apply force to it, then add the projectile to the collision channel. Also to spawn the projectile in the correct sockets, you can mimic the spawning logic of the turrets.
[/QUOTE]

Thank you very much. When I go on with the project, I meet some more problems. One of them is that after seeing a category called Blueprint Spawn,I use the picker and add in my Turret Blueprint as P1 shows. Then in the EditorWindow, I can see the ship and the turret combine into one in BP_Turret as P2 shows. The strange thing is that neither BP_Ship nor BP_Turret can show a whole vehicle after I drag them to the desert of my project one by one! They are seperated as I have done nothing! Could you please give me some advice? I am a beginner both in C++ and UE4.
P1

P2
2c388fd2b8d1fa8d5900b4d925e7a44f89b50797.jpeg

P3

For me I still can not use mouse controls, only the mousewheel zoom is working, no idea why.
Tried all checkboxes for controller / pawn rotation but no dice so far.
@scorpio_b I can only advice reading the whole tutorial again step by step and make sure you follow all the steps exactly.
Did you setup the sockets on the muzzle and “ship” mesh correctly?

Yes, I have setup the sockets on the muzzle and “ship” mesh correctly. I am checking the project now and going to read the tutorial again. How are you going?

Hey, sorry for my late response, I just got back from a camping trip.

Now let’s address your issue Scorpio_b. From what I understand your trying to drag the ship, out of game mode, in the editor. It will not show the turret on the chasis as the turret is spawned into the world when you start the game. Have you tried pressing play or simulate and see if the turret appears on the chasis? So to break it down, in your naming convention.

BP_Ship is configured in the blueprint details to spawn the BP_Turret you’ve selected. When you set the player spawn and set the vehicle as the player, or even drag the ship into the world and press play, the turret on BP_Ship should spawn in. I’m confused as to why your BP_Turret is showing it being connected to the chasis of the vehicle as that shouldn’t even happen. The BP_Turret should only show the turret itself.

I know it’s a weird concept as to spawning the turret into the game when you play, but it’s a way to enable having vehicles who have multitude of turrets that can be swapped out later during the game. There are a few different methods to get the turret and ship connected, and I could get a quick little tutorial showing you how do that with C++. (The more I think about it, the more I believe expanding the tutorial to show different methods may be a good example of how else it can be done, in case one way just isn’t working.)

So tldr; move the player vehicle into the world, press play, see if the turret spawns in.

Chairmen Meow: Can you move the ship at all even with the camera not moving? Put your input binding code into a block on here and I can step through and see if there’s something missing. A good thing to check is did you name the input bindings exactly the same as you did in the code?

Hey, no worries about “late” replies, it’s a forum not a chat :smiley:

Also now that you say it I can not move the ship and I also think I know where I screwed up.
I think I put everything into the c++ flying template.
I switched the default pawn to the ship one and gave the inputs the correct names while removing the flying template default controlls.
Still something in there is probably conflicting with your tutorial setup.
So I will try in a clean c++ project when I get time and report back.

Thanks for pointing me into the right direction!

Thank you for your guidance and patience. I can’t surf the Internet these day because my net was broken, so sorry for my late response. Now I can see both BP_Ship and BP_Turret when I click Play. But they were too far away from each other. Clearly this is not a problem. Now I am trying to make it fire as well.

At first, I cannot use mouse controls, either. Then I rewrite the code.


//void AShipBase::CameraYawInput(float Val)
//{
//	SpringArm->RelativeRotation.Yaw += Val;
//}

//void AShipBase::CameraPitchInput(float Val)
//{
//	float NewPitch = (Val * -1.0f) + SpringArm->RelativeRotation.Pitch;
//	SpringArm->RelativeRotation.Pitch = FMath::Clamp(NewPitch, CameraMinPitch, CameraMaxPitch);
//}

FRotator CurrentRate;  //I define it in the ShipBase.h

void AShipBase::CameraPitchInput(float Val)
{
	if (Val != 0)
	{
		CurrentRate.Pitch = FMath::Clamp(1.0f*Val, -1.0f, 1.0f);
		FRotator NewRotation = SpringArm->GetComponentRotation() + CurrentRate;
		if (NewRotation.Pitch < -42.0f)
			NewRotation.Pitch = -42.0f;
		if (NewRotation.Pitch > 2.f)
			NewRotation.Pitch = 2.f;
		SpringArm->SetWorldRotation(NewRotation);
	}
}

void AShipBase::CameraYawInput(float Val)
{
	if (Val != 0)
	{
		CurrentRate.Yaw= FMath::Clamp(-1.0f*Val,-1.0f, 1.0f);
		FRotator NewRotation = SpringArm->GetComponentRotation() + CurrentRate;
		SpringArm->SetWorldRotation(NewRotation);
	}
}

Hey scorpio, when posting code, encapsulate the code with the


 blocks. It's hard to read code when its not formatted in those code blocks. I'm going to take a look at the control logic again and make sure I didn't leave an error in the code.

Thanks for your reminding.

Any luck with the rebuilding?

Sadly I did not get around to test further.
As soon as I get time I will try it and give feedback if everything is working correctly.
Sorry for the delay!