How can I set the direction to a UProjectileMovementComponent?

I’m learning Unreal Engine developing Pong game.

Now I’m implementing ball movement using a UProjectileMovementComponent. This is in Ball’s constructor:

UProjectileMovementComponent* ProjectileMovement;

// Create our movement component.
// Use a ProjectileMovementComponent to govern this projectile's movement
ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("BallProjectileComp"));
ProjectileMovement->UpdatedComponent = CollisionComp;
ProjectileMovement->InitialSpeed = 30.f;
ProjectileMovement->MaxSpeed = 30.f;
ProjectileMovement->bRotationFollowsVelocity = false;
ProjectileMovement->bShouldBounce = true;
ProjectileMovement->Bounciness = 1.0f; // Do not lose velocity when bounces.

// Disable gravity.
ProjectileMovement->ProjectileGravityScale = 0.0f;

// Enable constraint movement to a plane.
ProjectileMovement->SetPlaneConstraintEnabled(true);

// Constraint direction to plane Y (so that X-Z cannot change).
// Done with a vector with X=1 and Z=1.
ProjectileMovement->SetPlaneConstraintNormal(FVector(1.0f, 0.0f, 1.0f));

But, when I spawn a ball in my GameStateBase class:

UWorld* const world = GetWorld();
if (world)
{
	world->SpawnActor<ABall>(ABall::StaticClass());
}

The ball moves in Z axis. In the editor I see move it to the sky.

What I am doing wrong? How can I make it move in Y axis?

Ok what you are trying here doesn’t work, because you are contraining the movement to a plane. A plane is two-dimensional, whereas you want to constrain the movement to an axis, which is one-dimensional. What you are currently doing is creating a plane on which your ball can move. Since this plane is defined by your normal, the ball is able to move in Y-direction, and it is also able to move in X-Z-direction, on an angle of 45° (try visualising the plane you created, this will help).

How to fix it: I am ashamed by it, but I can’t find the ■■■■■■■ C++ method for this, so I show you the blueprint way (which is more convenient most of the time anyway):

You need a blueprint class of your ABall Actor for this. Go to any SceneComonent in that blueprint class
and scroll to Physics in the Details Panel. You can constrain movement and rotation along axes by selection Six DOF as Mode. There is also a C++ Method for it, but I can’t find it right now!!!

123436-6dof.png

You now need to spawn the blueprint class, instead of your c++ class. To achive this you need a variable in one of your classes that looks like this:

UPROPERTY(EditAnywhere)
     TSubclassOf<ABall> BlueprintClass;

You can then edit this variable in the details panel again (Edge Class = your Blueprint Class):

123437-class.png

Set this to your Blueprint class. The last thing to do is to spawn the thing. This should do the trick:

GetWorld()->SpawnActor<ABall>(BlueprintClass);

I hope this helps.