Hi everyone, I am a new to this and get some problem.
I am using the First Persion Template to study. UE Version: 4.13
Cube A is created in constructor
UCLASS()
class FPERSION_API AMyCubeActor : public AActor
{
GENERATED_BODY()
public:
void AddCube(UStaticMeshComponent* mesh, FVector relativeLocation);
public:
// Sets default values for this actor's properties
AMyCubeActor();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Scenes)
class USceneComponent* Scene;
};
AMyCubeActor::AMyCubeActor()
{
// 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;
this->Scene = CreateDefaultSubobject<USceneComponent>(TEXT("Scene"));
RootComponent = this->Scene;
UStaticMeshComponent* StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("1M_Cube"));
ConstructorHelpers::FObjectFinder<UStaticMesh> meshToUse(TEXT("/Game/Geometry/Meshes/1M_Cube.1M_Cube"));
if (meshToUse.Succeeded() && StaticMesh)
{
StaticMesh->SetStaticMesh(meshToUse.Object);
StaticMesh->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
}
}
void AMyCubeActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMyCubeActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
The Cube B is created in First Persion Character 's void AfpersionCharacter::OnFire()
void AfpersionCharacter::OnFire()
{
FHitResult hitResult(ForceInit);
FCollisionQueryParams ccq(FName(TEXT("CombatTrace")), true, NULL);
ccq.bTraceComplex = true;
ccq.bTraceAsyncScene = false;
ccq.bReturnPhysicalMaterial = false;
ccq.AddIgnoredActor(this);
const FRotator Rotation = GetControlRotation();
const FRotator YawRotation(Rotation.Pitch, Rotation.Yaw, 0);
FVector pos = GetActorLocation();
pos = FirstPersonCameraComponent->GetComponentLocation();
FRotator _YawRotation = FirstPersonCameraComponent->GetComponentRotation();
FVector dir = FRotationMatrix(_YawRotation).GetUnitAxis(EAxis::X);
FVector posBegin = pos;
FVector posEnd = pos + dir * 5000;
GetWorld()->LineTraceSingleByChannel(hitResult,
posBegin,
posEnd,
ECC_WorldStatic,
ccq);
DrawDebugLine(this->GetWorld(), posBegin, hitResult.Location, FColor(1.0f, 1.f, 0.f, 1.f), false, 20.f);
if(hitResult.bBlockingHit)
{
DrawDebugSphere(GetWorld(), hitResult.Location, 10, 10, FColor::Red, false, 20.f);
AMyCubeActor* tmpA = dynamic_cast<AMyCubeActor*>(hitResult.GetComponent()->GetAttachmentRootActor());
if ((AMyCubeActor*)0 != tmpA)
{
UStaticMesh* _mesh= (UStaticMesh*)StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/Geometry/Meshes/1M_Cube.1M_Cube"), TEXT("/Game/Geometry/Meshes/1M_Cube.1M_Cube"), LOAD_MemoryReader);
UStaticMeshComponent* _StaticMesh = NewObject<UStaticMeshComponent>();
if (_mesh&& _StaticMesh)
{
_StaticMesh->SetStaticMesh(_mesh);
_StaticMesh->SetHiddenInGame(false);
_StaticMesh->MarkRenderStateDirty();
_StaticMesh->SetVisibility(true);
_StaticMesh->SetWorldLocation(hitResult.Location + hitResult.Normal * 70.0f);
_StaticMesh->BodyInstance.SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
_StaticMesh->BodyInstance.SetObjectType(ECC_WorldStatic);
_StaticMesh->BodyInstance.SetResponseToChannel(ECC_WorldDynamic, ECR_Block);
_StaticMesh->BodyInstance.SetResponseToChannel(ECC_Pawn, ECR_Block);
_StaticMesh->RegisterComponentWithWorld(this->GetWorld());
_StaticMesh->AttachToComponent(tmpA->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);
_z += 100.0f;
}
}
}
}
}
The problem is that when character jump to the top of Cube A, character can walk normally. But when the Character jump to top of Cube , the character can not walk any more but move a little distance after character jump on Cube B.
so, am i forget something?