The Tank Turret is not rotation

Hello Everyone i came with new issue the turret is not rotation , i dont why , can some one help me into this ,i 'll share everything i have

here is the all i share let me know if you need anything

i am waiting

Hey Joshi,

Couple things to get you moving.

Firstly, when submitting code with your problem it will be much much easier for people to read it if you paste the text itself into a code block instead of pasting screenshots of the code. Check out this link

Second, I would recommend renaming your ProjectileRef variable in Tank.h with a more accurate description. Perhaps PlayerControllerRef or TankPlayerController etc… I assume this is a copy and paste error but reading the code is made that much more difficult when variables don’t describe their purpose. If you do change the name remember that if you don’t use the editor’s auto renaming functionality you’ll need to change the rest of the places where you use that variable to match the new name. If you’re not sure you’ve gotten them all. You can try to compile and the compiler should get mad at you if you’ve missed any :slight_smile:

Finally, onto the problem. I see you’ve tried to add some debug logging into these functions;

ABasePawn::RotateTurret

ATank::Tick

This is a great idea for trying to backtrack through the logic and figure out where the “hole” is in the logic. Unfortunately since both of your prints are printing the same thing it makes it harder to reason about what exactly is and is not working.

I’d recommend changing the prints and perhaps adding some more. Here’s a rough idea of what you could add. NOTE This is just partial code and I haven’t check/compiled it so there could be typos or errors. (It’s much harder to copy/paste/modify your example code when it is in screenshot form :wink: )

ATank::Tick(float DeltaTime)
{
        UE_LOG(LogTemp, Warning, TEXT("Updaing our tank in general"));
        if(ProjectileRef)
        {
            UE_LOG(LogTemp, Warning, TEXT("We have a legitimate controller reference"));
            ProjectileRef->GetHitResult... blah blah blah (CHANGE PROJECTILEREF NAME :) )
            UE_LOG(LogTemp, Warning, TEXT("Mouse Cursor Hit Result Location is %s"), *HitResult.ImpactPoint.ToString());
            RotateTurret(HitResult.ImpactPoint)
        }
}

then in BasePawn

ABasePawn::RotateTurret(FVector Locate)
{
        UE_LOG(LogTemp, Warning, TEXT("Trying to rotate the turret towards %s"), *Locate.ToString());
    ... rest of the code

If you try these and let us know what sort of prints you’re getting then it will give us some more insight into the problem.

If you’re not seeing your tank tick at all then we’re not creating it or have disabled updates. Something like that.

If your ticking, but not seeing anything else, then we’ve probably failed to get the player controller.

If you’re ticking, getting a hit point and it’s some crazy numbers something else is happening.

etc…

Hopefully you’re still around and find some of this useful, or maybe you’ve already solved it for yourself by now. Let me know.

All the best!

1 Like

Hi Joshi,
I don’t actually see anywhere in the code where the turret rotation is handled - this is related to the mouse and the position is determined by calling GetHitResultUnderCursor. This is handled in the Tank.cpp in the Tick.

Have you checked your code against the lecture changes? The code is added in the Lecture Rotating the Turret but I see you’ve posted your question at the end of the section.

I suspect you’ve missed some code.

I think he has the code block alright for rotation.
ABasePawn::RotateTurret(FVector Locate)

It’s at the bottom of this image

And ATank derives from ABasePawn so it should call up to that.

He had the method but wasn’t being called anywhere. In tank, code responds to the mouse position, in turret it is where the tank is positioned and points at the tank.

The code to position the actual turret is the same for both and located in the base pawn but the method is different to actually tell it when to rotate.

1 Like

Ah I see what you’re saying beegeedee.

I assumed Joshi was talking about the Tank’s turret. If you’re talking about the hostile red “tower” turrets not aiming towards your tank then you’re missing that code altogether Joshi (Or at least haven’t provided any screenshots of the implementation). If you’re talking about the turret on top of the tank that you control as the player not aiming, then we’d need some more info.

For the hostile turrets as beegeedee is saying, you should have a separate c++ file for their implementation with something like this

void ATower::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    if(InFireRange())
    {
        RotateTurret(Tank->GetActorLocation());
    }
}

He was talking about the tank. The rotate turret method is for both, but the code in Tick is different for both. He was asking about the tank and why it didn’t rotate the turret but the code in tank.cpp for the tick wasn’t there.

The turrets come later but the same method in the base class is used.

Oh okay. Isn’t that code right here in the first image of the 2nd post?

ATank::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
    UE_LOG(LogTemp, Warning, TEXT("Updaing our tank in general"));
    if(ProjectileRef)
    {
        FHitResults HitResult;

        ProjectileRef->GetHitResultUnderCursor(...insert arguments...);
        RotateTurret(HitResult.ImpactPoint)
        UE_LOG(LogTemp, Warning, TEXT("It's Working"));
    }
}

No, you are right. It is there.

The code isn’t 100% correct though as the screen check for the mouse returns a boolean and should only be acted upon when true.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms