# C++: Spawn and move vehicles (controller-\>car)

**URL:** <https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237>\
**Category:** World Creation\
**Tags:** vehicle, car, tsubclassof, unreal-engine, Physics, UE4, CPP, question\
**Created:** [May 3, 2018, 12:00pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237 "2018-05-03T12:00:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lunarix\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lunarix\_1](https://forums.unrealengine.com/u/Lunarix_1)\
**Post date:** [May 3, 2018, 12:00pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/1 "2018-05-03T12:00:51Z")

</div>

Hello,  
i try to set up a controller → car set up, so that I can control every car in every frame with the controller.  
My car is driving when it is placed without the controller.  
It also works for a moving cube(with the controller), but not for cars. I do not get what I’m doing wrong.

This is part of my .h of the controller:

```
std::vector<AMyPawn*> car_vector;
UPROPERTY(EditAnywhere, Category = "Spawned Car")
TSubclassOf<AMyPawn> TSubClass_SpawnedCar;

```

This is the controller .cpp part in the “begin” function:

```
   AMyPawn* obj = GetWorld()->SpawnActor<AMyPawn>(TSubClass_SpawnedCar,our_loc,FRotator::ZeroRotator);
   car_vector.push_back(obj);

```

The car spawns and looks correct.  
Every tick I’d like to control its movemnt:

```
for(std::vector<AMyPawn*>::iterator it = car_vector.begin(); it != car_vector.end(); it++)
    {
        FVector loc = (*it)->GetActorLocation();
        FString d_string = loc.ToString();
        UE_LOG(LogTemp,Log,TEXT("Car %i at position: %s"),counter,*d_string);

         //Calculate movement for vehicle
        (*it)->MoveForward(1.0f);
    }

```

MoveForward is declared in the car’s cpp:

```
void AMyPawn::MoveForward(float Val)
{
    UE_LOG(LogTemp,Log,TEXT("Throttle input set to: %f"),Val);
    GetVehicleMovementComponent()->SetThrottleInput(Val);
}

```

Everything works until here - the ThrolleInput is set to 1 - but the car does not move. It applies possible physics (drops from the air to the ground) but does not move. What am I doing wrong?

Im glad for any help!  
thanks a lot!

---

<div class="post-metadata">

**Author:** ![MrMcKvarz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MrMcKvarz](https://forums.unrealengine.com/u/MrMcKvarz)\
**Post date:** [May 3, 2018, 4:23pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/2 "2018-05-03T16:23:45Z")

</div>

Hi, SetThrottleInput will only work with an actor with a controller, you need to either create a controller for each car or move them directly.

---

<div class="post-metadata">

**Author:** ![Lunarix\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lunarix\_1](https://forums.unrealengine.com/u/Lunarix_1)\
**Post date:** [May 3, 2018, 4:50pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/3 "2018-05-03T16:50:03Z")

</div>

Hi, im sorry I do not really understand that - it did not even work just for one car. I can put the moveForward function in the tick of the car-class aswell and nothing happens. what do you mean with move them directly? any specific function? im pretty new trying to understand this problem.  
anyway, thanks for your response:)

---

<div class="post-metadata">

**Author:** ![MrMcKvarz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MrMcKvarz](https://forums.unrealengine.com/u/MrMcKvarz)\
**Post date:** [May 3, 2018, 6:02pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/4 "2018-05-03T18:02:21Z")

</div>

SetThrottleInput works with APlayerController. For testing, create 1 car and do posses in a [controller](http://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/APlayerController/Possess/index.html). Then you should be able to move your car.

---

<div class="post-metadata">

**Author:** ![Lunarix\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lunarix\_1](https://forums.unrealengine.com/u/Lunarix_1)\
**Post date:** [May 3, 2018, 6:10pm UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/5 "2018-05-03T18:10:51Z")

</div>

Ok ill test it in 10h from now.  
So I change my Acotor (Controller now) to a APlayerController and try Posses the car? Ill give it a try then… hope it works

---

<div class="post-metadata">

**Author:** ![Vinayaka\_P](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Vinayaka\_P](https://forums.unrealengine.com/u/Vinayaka_P)\
**Post date:** [February 12, 2020, 10:23am UTC](https://forums.unrealengine.com/t/c-spawn-and-move-vehicles-controller-car/424237/6 "2020-02-12T10:23:20Z")

</div>

This thread might help: [Cannot create vehicle... UpdatedComponent has not initialized its rigid body actor - Character & Animation - Unreal Engine Forums](https://answers.unrealengine.com/questions/947133/view.html)
