Download

Hello, I would like to create a head bobble effect with C++.

Hello, as the title suggests. I want to create a head bobble effect for a first-person character in c++. However, I am decently new to c++ I am transferring from Lua with slight prior experience in HTML and other coding languages. How can I achieve this effect in the up most efficient and quality way possible?

Two ways, Vector Graph or Camera shake. For example my movement sway for the firearm is handled in the same way I would handle headbob via graph. Make a Vector Graph and setup an accumulator in a fashion similar to this. VelocityMultiplier is a float thats normalized using the characters velocity between 0 and the max speed of the character (default 600) so it amplifies smoothly between idle/walking/running

Accumulator += (DeltaSeconds * VelocityMultiplier);
FVector Graph = Curve->GetVectorValue(Accumulator );
Graph *= VelocityMultiplier * Multiplier;
		
CameraLocation = Graph;

The other route is via CameraShake which is simpler IMO. You would do something similar by normalizing the Characters Velocity and plugging that into the scale in PlayCameraShake so that way when your not moving its at 0 so theres no camera shake and when you start moving it scales with your velocity.

1 Like

So a lot of this doesn’t make sense to me I have created a new project with axis mappings made a character class coded it to walk and look around I’m curious how I implement this code you’ve given me? e.g this ‘Accumulator’ variable do I make it a float or?

The accumulator is a stored float that basically keeps counting on an infinite graph so on each tick i take DeltaSeconds and multiply it by the velocity of the character and add that value to the accumulator as shown above. I then use that Accumulator to find a point on the graph to use like on line 2. Then I simply multiply that vector (gotten by line 2) by a normalized float of my velocity (ranges from 0 to 1) which is retrieved like so

VelocityMultiplier = UKismetMathLibrary::NormalizeToRange(CharacterVelocity, 0.0f, Character->GetSprintSpeed());

Now you have a point on the graph and how extreme you want it to be via the multiplier thats based on speed. From here move the camera around based on those values. Again the simpler route would be camera shake and just scale it based on velocity (ignore the Z axis)

1 Like

So, as I said I’m a complete beginner so new that im new to delta time and all that so how excactly would I get all these values? I made the Accumulator a float by doing float Accumulator. But I’m unsure on the DeltaSeconds, VelocityMultiplier, VelocityMultiplier, Multiplier.

DeltaSeconds can be best explained here so we dont get different results based on framerate Unreal Engine 4 Blueprints Tutorial | raywenderlich.com
VelocityMultiplier is just from this line. Its a normalized value (from 0 to 1). So you get your characters velocity as the first parameter, then the second parameter is the minimum value you want it to be normalized to and the third parameter is the maximum value you want it to be normalized to (your MaxWalkSpeed from character movement component) so something like Character->GetMovementComponent()->GetMaxWalkSpeed()

VelocityMultiplier = UKismetMathLibrary::NormalizeToRange(CharacterVelocity, 0.0f, Character->GetSprintSpeed());

1 Like

Hate to bother you again I’m struggling to get delta time is there a sure method to getting it? People are saying to use this; FApp::GetDeltaTime | Unreal Engine Documentation but when I use the include and try the syntax nothing works.

Also for the velocity, you said the character movement speed correct? Well my first-person character doesn’t handle movement speed like that I don’t think I use axis bindings and pass in integers to decide if the player moves forward and what not.

Alright sorry to make so many replies and questions but I took a couple more shots at it and I got it to this now

double VelocityMultiplier = UKismetMathLibrary::NormalizeToRange(CharacterVelocity, 0.0f, Character->GetSprintSpeed());
float Accumulator = (FApp::GetDeltaTime() * VelocityMultiplier );

The problem is I don’t know how to get the character’s velocity or reference the character. I use a character class for the player so would I use a this. Also in my previous reply, I talked about how I use axis mappings to move the character. e.g pressing w will give the value of 1 and pressing s will give the value of -1. Then how would I decide velocity would I use these values?

You can get the delta seconds from the world as well so GetWorld()->GetDeltaSeconds(). Where are you running this at? In the character class or?

Yes this is the character class cpp file.

Well then you already have your character reference. From there you can just get the Character Movement Component and get the velocity from that.

Wait so I figured some stuff out is this right?
CapsuleComponent->GetComponentVelocity();
But I cant get it I think because its in a private field it says member is inaccessible private field.

also is FApp::GetDeltaTime(); any correlation with getting delta time? Or is this any correlation virtual void Tick(float DeltaTime) override;

I suggest you take some time off and focus learning a bit more c++ outside of UE to get a bit more comfortable with classes. It would be from your character movement component. I would do this in your Tick function, you already have access to DeltaTime there.

1 Like

Alright I believe I am decently familiar with c++ but just not functions and macros prebuilt inside of the unreal engine game engine.