# Why is my movement script not working?

**URL:** <https://forums.unrealengine.com/t/why-is-my-movement-script-not-working/339959>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 28, 2015, 6:29pm UTC](https://forums.unrealengine.com/t/why-is-my-movement-script-not-working/339959 "2015-11-28T18:29:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RJHelms84](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RJHelms84](https://forums.unrealengine.com/u/RJHelms84)\
**Post date:** [November 28, 2015, 6:29pm UTC](https://forums.unrealengine.com/t/why-is-my-movement-script-not-working/339959/1 "2015-11-28T18:29:41Z")

</div>

I’m trying to make a small glass fly towards the player. The glass manages to raise up along the Z axis, but then the engine just crashes with no error messages. Am I doing something badly in my code, which could be causing the crash?

```
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "DOMUSCharacter.h"
#include "FlyingJunkEnemy.generated.h"

UCLASS()
class DOMUS_API AFlyingJunkEnemy : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AFlyingJunkEnemy();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	UFUNCTION(BlueprintCallable, Category = "Events")
	void StartJunkMovementAnim();

private:
	bool junkmovementBegin;
	bool notAtTarget;
	bool doOnce;
	ACharacter* playerCharacter;
	UBoxComponent* BoxComponent;
	FVector junkStartLocation;
	FVector temp;
	FVector distanceVector;
	FVector playerLoc;
	FVector junkLoc;
};

// Fill out your copyright notice in the Description page of Project Settings.

#include "DOMUS.h"
#include "FlyingJunkEnemy.h"

// Sets default values
AFlyingJunkEnemy::AFlyingJunkEnemy()
{
 	// 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;

	//init bool used in movement anim
	junkmovementBegin = false;

	//bool to check if junk is at target (player)
	notAtTarget = true;
	doOnce = false;
	temp = this->GetActorLocation();
	distanceVector = { 0.0f, 0.0f, 0.0f };
	playerLoc = { 0.0f, 0.0f, 0.0f };
	junkLoc = { 0.0f, 0.0f, 0.0f };

	//get player character
	playerCharacter = UGameplayStatics::GetPlayerCharacter(GetWorld(), 0);

	//get player location as FVector (do this dynamically as needed, as value will change)
	//playerCharacter->GetActorLocation();

	//get starting junk enemy location
	junkStartLocation = this->GetActorLocation();

	//create root component as cube with collision
	BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("RootComponent"));
	RootComponent = BoxComponent;
	BoxComponent->InitBoxExtent(FVector(5.0f, 5.0f, 5.0f));
	BoxComponent->SetCollisionProfileName(TEXT("Pawn"));
}

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

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

		//run when StartJunkMovementAnim function is called from blueprint
		if (junkmovementBegin)
		{
				if (this->GetActorLocation().Z < (junkStartLocation.Z + 300))
				{
					//do this until junk moves 30 up Z axis
					temp = { this->GetActorLocation().X, this->GetActorLocation().Y, this->GetActorLocation().Z + 0.2f };
					this->SetActorLocation(temp);
				}
				else if (notAtTarget)
				{
					//doonce
					if (!doOnce)
					{
						playerLoc = { playerCharacter->GetActorLocation().X, playerCharacter->GetActorLocation().Y, 0.0f };
						junkLoc = { junkStartLocation.X, junkStartLocation.Y, 0.0f };
						distanceVector = junkLoc - playerLoc;
						distanceVector.Normalize();
						distanceVector.X *= 0.5f;
						distanceVector.Y *= 0.5f;
						doOnce = true;
					}

					//each frame
					this->SetActorLocation(this->GetActorLocation() + distanceVector);

					if (FMath::Abs(this->GetActorLocation().X - playerCharacter->GetActorLocation().X) < 200)
					{
						if (FMath::Abs(this->GetActorLocation().Y - playerCharacter->GetActorLocation().Y) < 200)
						{
							notAtTarget = false;
						}
					}
				}
				else
				{
					junkmovementBegin = false;
					BoxComponent->SetSimulatePhysics(true);
				}
		}
}

void AFlyingJunkEnemy::StartJunkMovementAnim()
{
	//begin movement defined in 'Tick' function
	junkmovementBegin = true;
}
```

---

<div class="post-metadata">

**Author:** ![RJHelms84](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@RJHelms84](https://forums.unrealengine.com/u/RJHelms84)\
**Post date:** [November 28, 2015, 7:16pm UTC](https://forums.unrealengine.com/t/why-is-my-movement-script-not-working/339959/2 "2015-11-28T19:16:29Z")

</div>

Nevermind, someone pointed out that the playerCharacter should be defined after the engine has loaded, as it was being defined in the constructor before the player was even spawned.

---

<div class="post-metadata">

**Author:** ![Zarkopafilis](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zarkopafilis](https://forums.unrealengine.com/u/Zarkopafilis)\
**Post date:** [November 28, 2015, 7:35pm UTC](https://forums.unrealengine.com/t/why-is-my-movement-script-not-working/339959/3 "2015-11-28T19:35:57Z")

</div>

Indeed, engine should be the first defined thing everywhere
