# Third person navigation to navigate a car

**URL:** https://forum.castle-engine.io/t/third-person-navigation-to-navigate-a-car/571
**Category:** 3D
**Created:** [April 20, 2022, 6:22pm UTC](https://forum.castle-engine.io/t/third-person-navigation-to-navigate-a-car/571 "2022-04-20T18:22:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Didi](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/didi/32/1908_2.png) [@Didi](https://forum.castle-engine.io/u/Didi)
#### Post date: [April 20, 2022, 6:22pm UTC](https://forum.castle-engine.io/t/third-person-navigation-to-navigate-a-car/571/1 "2022-04-20T18:22:56Z")

</div>

Hi there,

i use the third person navigation to navigate a car in my racing game.  
my car is the avatar.

following works fine:  
ThirdPersonNavigation.Input\_LeftRotate.Assign(keyArrowLeft);  
ThirdPersonNavigation.Input\_RightRotate.Assign(keyArrowRight);

but i use one button for moving forward:  
ThirdPersonNavigation.Input\_Forward.Assign(keyR);

with arrowup and arrowdown i increase or decrease ThirdPersonNavigation.MoveSpeed.  
when i stop pushing up (down is to break), the car is moving forward with very slow deceleration, like a car in real world would do.  
my problem is, that i have to hold the keyR down all the time. after 10 rounds my finger hurts!

maybe someone has an idea how this could be improved.

thanks in advance  
Didi

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [April 21, 2022, 1:14pm UTC](https://forum.castle-engine.io/t/third-person-navigation-to-navigate-a-car/571/2 "2022-04-21T13:14:50Z")

</div>

I think in this case you can move the car directly, following any logic you like, without depending on built-in input shortcuts `Input_Xxx` in TCastleThirdPersonNavigation.

That is, inside the `Update` method of your state, just check for something and move the car directly, like

```delphi
procedure TStateMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
const
  CarSpeed = 5.0; // units per second
begin
  inherited;
  if CarMoving then
    Car.Move(Car.Direction * SecondsPassed * CarSpeed);
end;

function TStateMain.Press(const Event: TInputPressRelease): Boolean;
begin
  Result := inherited;
  if Result then Exit; // allow the ancestor to handle keys
 
  if Event.IsKey(keySomethingToStart) then
  begin
    CarMoving := true;
    Exit(true); // event was handled
  end;

  if Event.IsKey(keySomethingToStop) then
  begin
    CarMoving := false;
    Exit(true); // event was handled
  end;
end;

```

where `Car` is the `TCastleTransform` , the same thing you use as `AvatarHierarchy` or `Avatar` in `TCastleThirdPersonNavigation`.

(see [Designing user interface and handling events (press, update) within the state | Manual | Castle Game Engine](https://castle-engine.io/manual_state_events.php) about state methods Update and Press).

Internallly, `TCastleThirdPersonNavigation.Update` does mostly the same. It calculates `A` as

```delphi
function TCastleThirdPersonNavigation.RealAvatarHierarchy: TCastleTransform;
begin
  if AvatarHierarchy <> nil then
    Result := AvatarHierarchy
  else
    Result := Avatar;
end;

```

and then does

```delphi
...
    if Input_Forward.IsPressed(Container) then
    begin
      Moving := true;
      T := T + A.Direction * Speed * SecondsPassed;
    end;
...
    if not T.IsPerfectlyZero then
      A.Move(T, false);
...

```

---

<div class="post-metadata">

### Author: ![Didi](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/didi/32/1908_2.png) [@Didi](https://forum.castle-engine.io/u/Didi)
#### Post date: [April 25, 2022, 1:52pm UTC](https://forum.castle-engine.io/t/third-person-navigation-to-navigate-a-car/571/3 "2022-04-25T13:52:21Z")

</div>

thank you very much, the navigation works now how it should.
