Changes on UProjectileMovemenentComponent.Velocity does not affect its direction

ViaCognita
Fri, 27 Jan 2017 06:47:12 +0000
World Creation

I’m learning Unreal Engine and I’m developing Pong game to learn a lot!!

I have a problem, it I change UProjectileMovemenentComponent.Velocity in OnHit event it doesn’t affect it.

Here is my class:

ABall::ABall()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	// Our root component will be a box that reacts to physics and interact with physical world.
	UBoxComponent* CollisionComp = CreateDefaultSubobject<UBoxComponent>(TEXT("BallBoxComponnent"));
	RootComponent = CollisionComp;

	// This extents the box from the origin. The value is 10.0f because the static mesh size is 20.0f.
	// And the distance from the origin, that is in the middle of box, to box's sides is 20.0f /2.0f = 10.0f.
	CollisionComp->InitBoxExtent(FVector(10.0f, 10.0f, 10.0f));

	// Create a mesh componnent so we ca see where our ball is.
	UStaticMeshComponent* BallVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BallVisualRepresentation"));
	BallVisual->SetupAttachment(RootComponent);

	// Enable collisions
	SetActorEnableCollision(true);
	CollisionComp->SetCollisionProfileName(TEXT("BlockAll")); // or BoxComponent->SetCollisionResponseToAllChannels(ECollisionResponse::ECR_Block);
	// Set up a notification for when this component hits something blocking.
	CollisionComp->OnComponentHit.AddDynamic(this, &ABall::OnHit);

	// Add a visual cuboid to see it.
	static ConstructorHelpers::FObjectFinder<UStaticMesh> CuboidVisualAsset(TEXT("'/Game/Meshes/SM_Ball.SM_Ball'"));
	if (CuboidVisualAsset.Succeeded())
	{
		BallVisual->SetStaticMesh(CuboidVisualAsset.Object);
		static ConstructorHelpers::FObjectFinder<UMaterial> WhiteMaterial(TEXT("'/Game/Materials/M_White.M_White'"));
		if (WhiteMaterial.Succeeded())
		{
			BallVisual->SetMaterial(0, WhiteMaterial.Object);
		}
	}

	// Create our movement component.
	// Use a ProjectileMovementComponent to govern this projectile's movement
	ProjectileMovement = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("BallProjectileComp"));
	ProjectileMovement->UpdatedComponent = CollisionComp;
	ProjectileMovement->InitialSpeed = 1500.f;
	ProjectileMovement->MaxSpeed = 1500.f;
	ProjectileMovement->bRotationFollowsVelocity = true;
	ProjectileMovement->bShouldBounce = true;
	// Do not lose velocity when bounces.
	ProjectileMovement->Bounciness = 1.0f;
	// Set velocity vector to move ball to -Y axis.
	ProjectileMovement->Velocity = FVector(0.0f, -1.0f, 1.0f);

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

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

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

// Called when the game starts or when spawned
void ABall::BeginPlay()
{
	Super::BeginPlay();

	Rename(TEXT("Ball"));
}

// Called every frame
void ABall::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
}

void ABall::OnHit(UPrimitiveComponent * HitComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
{
	if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
	{
		ProjectileMovement->Velocity.Z = 1.0f;
	}
}

Maybe the problem is that I’m using bShouldBounce = true.

Any idea about how it doesn’t change its Velocity?

2 posts - 1 participant