Change Gravity

I’m tryin to change the gravity direction and so far I managed to do that but I can’t change it in real time during the game. So far I have the following code in my game class:

void ABallScrollBall::SetupPlayerInputComponent(class UInputComponent* InputComponent) {
        	// some stuff
        
        	InputComponent->BindAction("GravityRight", IE_Pressed, this, &ABallScrollBall::GravityRight);
        	InputComponent->BindAction("GravityLeft", IE_Pressed, this, &ABallScrollBall::GravityLeft);
        	InputComponent->BindAction("GravityUp", IE_Pressed, this, &ABallScrollBall::GravityUp);
        	InputComponent->BindAction("GravityDown", IE_Pressed, this, &ABallScrollBall::GravityDown);
        }
        
        void ABallScrollBall::GravityRight() {
        	//call ChGrav(1);
        }
        
        void ABallScrollBall::GravityLeft() {
        	//call ChGrav(2);
        }
        void ABallScrollBall::GravityUp() {
        	//call ChGrav(3);
        }
        void ABallScrollBall::GravityDown() {
        	//call ChGrav(4);
        }

In the PhysLevel.cpp:

void UWorld::SetupPhysicsTickFunctions(float DeltaSeconds)
{
	// stuff

	//FVector DefaultGravity( 0.f, 0.f, GetGravityZ() ); was the default

        //stuff
}

void UWorld::ChGrav(short dir) {
	switch (dir) {
	case 1:
		DefaultGravity = FVector(0.f, -500.f, 0.f);
		break;
	case 2:
		DefaultGravity = FVector(0.f, 500.f, 0.f);
		break;
	case 3:
		DefaultGravity = FVector(0.f, 0.f, 500.f);
		break;
	default:
		DefaultGravity = FVector(0.f, 0.f, -500.f);
	}
}

And in the World.h

//stuff
public:
	FVector DefaultGravity;
	void ChGrav(short dir);
//stuff

(i think) The problem is that I can’t access the UWorld class.

Hey, you might want to try putting the following code in a separate function and call on it when necessary. Dunno if it’s the answer or not but it’s a suggestion. Also you don’t need to specify the return type when calling a function. Just the class name and method. :slight_smile:

Instead just call these when moving in any of these directions.

 ABallScrollBall::GravityRight() {
//call ChGrav(1);
}
ABallScrollBall::GravityLeft() {
//call ChGrav(2);
}
ABallScrollBall::GravityUp() {
//call ChGrav(3);
}
ABallScrollBall::GravityDown() {
//call ChGrav(4);
}

Howdy Karwler!

I am trying to accomplish the same thing as you (change gravity direction in-game), were you ever able to figure this out? If so, could you share how you did it?

DuhCent

Unfortunately I still have some trouble with it. Everything is set up perfectly but I have no idea how to access the current running UWorld instance.

Thanks for the reply Karwler!

That stinks that you are still struggling with getting the gravity set in game.

I have recruited some friends to help solve the issue. If we get it figured out I will be sure to reply to this post and give a step by step on how we did it.

DuhCent

I recently made a pull request that does this.

You can simply call SetWorldGravity from the Level Blueprint and everything will change. If you don’t want your character moving around set its custom gravity field to whatever you want.

See my answer