# player

### Methods

* [getPosition](#player.getposition)
* [setPosition](#player.setposition-position)
* [getVelocity](#player.getvelocity)
* [setVelocity](#player.getvelocity)
* [getView](#player.getview)
* [setView](#player.setview-viewangles)
* [getDeltas](#player.getdeltas)
* [getCrosshairWorldPosition](#player.get2dspeed)
* [get2DSpeed](#player.get2dspeed)
* [getCurrentWeaponID](#player.getcurrentweaponid)
* [setCurrentWeaponID](#player.setcurrentweaponid-id)
* [getFPS](#player.getfps)
* [setFPS](#player.getfps-1)
* [lookAtPosition](#player.lookatposition-position)
* [moveTo](#player.lookatposition-position-1)
* [getName](#player.lookatposition-position-1)
* [getMainhandWeaponID](#player.getname-1)
* [getRPGWeaponID](#player.getname-2)
* [getWeaponDelay](#player.getname-3)
* [isConnectedToServer](#player.getname-4)
* [isOnLocalServer](#player.getname-5)
* [isSpectating](#player.getname-6)
* [isUsingRPG](#player.getname-7)
* [willLeaveGroundThisFrame](#player.getname-8)
* [willTouchGroundThisFrame](#player.getname-9)
* [willTouchGroundNextFrame](#player.getname-10)
* [wasInAirLastFrame](#player.getname-11)
* [didTouchGroundLastFrame](#player.getname-12)
* [willBounceThisFrame](#player.getname-13)
* [willBounceNextFrame](#player.getname-14)

### <mark style="color:green;">`player.getPosition()`</mark>

Returns your position as a Vec3

**Example:**

```lua
local position = player.getPosition()
print(position:toString()
```

### <mark style="color:green;">`player.setPosition(position)`</mark>

{% hint style="warning" %}
Only works in devmap.
{% endhint %}

<mark style="color:purple;">`position`</mark> - The position you want to set

Sets your position to the given [Vec3](/types/vec3.md)

**Example:**

```lua
player.setPosition(player.getPosition():add(Vec3:new(0, 0, 1000))
-- Teleport up 1000 units
```

### <mark style="color:green;">`player.getVelocity()`</mark>

Returns your velocity as a Vec3.

**Example:**

```lua
local velocity = player.getVelocity()
print(velocity:toString())
```

### <mark style="color:green;">`player.setVelocity(velocity)`</mark>

{% hint style="warning" %}
Only works in devmap.
{% endhint %}

<mark style="color:purple;">`velocity`</mark> - The velocity you want to set

Sets your velocity to the given [Vec3](/types/vec3.md)

**Example:**

```lua
player.setVelocity(Vec3:new(3000, 3000, 3000))
```

### <mark style="color:green;">`player.getView()`</mark>

Returns your viewangles as a Vec3 in the following format

1. pitch
2. yaw
3. roll

**Example:**

```lua
local viewangles = player.getView()
print(tostring(viewangles.y) -- Prints your yaw angle
```

### <mark style="color:green;">`player.setView(viewAngles)`</mark>

* <mark style="color:purple;">`viewAngles`</mark> - A Vec3 with the angles you wish to set

Set your viewangles to the ones defined by the given Vec3

{% hint style="info" %}
Adding onto angles indicates leftward motion, subtracting rightward motion
{% endhint %}

**Example:**

```lua
-- Spin left
function turn()
    local view = player.View()
    view.y = view.y + 1
    player.setView(view)
end

engine.addEventListener("render", turn);
```

### <mark style="color:green;">`player.getDeltas()`</mark>

Returns your delta angles as a Vec3.<br>

{% hint style="info" %}
Delta angles can be hard to wrap your head around at first.\
Call of Duty 4 does not update your viewangle in memory if a teleport occurs.\
Teleports such as loading your position in CodJumper will create a mismatch of your true viewangle and the one that resides in memory.&#x20;

The difference is called the **delta angle.**<br>

**Example:**

If you save your position with a yaw angle of <mark style="color:green;">0</mark> and turn <mark style="color:green;">180</mark> degree around,

then your yaw angle in memory will be <mark style="color:green;">180</mark>. If you then load your position

it will still be <mark style="color:green;">180</mark>, however the yaw delta angle will be <mark style="color:green;">-180</mark>.

Together they make up your true angle of yaw <mark style="color:green;">0</mark>.

**Delta angles go from&#x20;**<mark style="color:green;">**-180 to +180**</mark>
{% endhint %}

**Example:**

```lua
local deltas = player.getDeltas()
print(deltas:toString()) -- Your delta angles
```

### <mark style="color:green;">`player.getCrosshairWorldPosition()`</mark>

Returns the position the player currently aims at.

**Example:**

```lua
local aimPosition = player.getCrosshairWorldPosition()
print("I am aiming at " .. aimPosition:toString())
```

### <mark style="color:green;">`player.get2DSpeed()`</mark>

Returns your current 2D speed.

**Example:**

```lua
local speed = player.get2DSpeed()
print("I am " .. speed .. " units fast")
```

### <mark style="color:green;">`player.getCurrentWeaponID()`</mark>

Returns the weapon ID of your currently equipped weapon.

**Example:**

```lua
local weaponID = player.getCurrentWeaponID()
print(weaponID)
```

### <mark style="color:green;">`player.setCurrentWeaponID(id)`</mark>

* <mark style="color:purple;">`id`</mark> The id of the weapon you want to switch to

Switches weapon to the given weapon ID.

**Example:**

```lua
local position = player.getPosition()
print(position:toString())
```

### <mark style="color:green;">`player.getFPS()`</mark>

Returns your current FPS.

**Example:**

```lua
local fps = player.getFPS()
print("Current fps: " .. fps)
```

### <mark style="color:green;">`player.setFPS(fps)`</mark>

* <mark style="color:purple;">`fps`</mark> - The new fps to set

Sets your fps to the given value.

**Example:**

```lua
player.setFPS(250)
```

### <mark style="color:green;">`player.lookAtPosition(position)`</mark>

* <mark style="color:purple;">`position`</mark> - A position as a Vec3 that you want to look at

Sets your viewangles so that you directly look at the given `position`

**Example:**

```lua
local closestPlayer = engine.getClosestPlayer()

-- Makes you look at the closest player
player.lookAtPosition(closestPlayer.origin)
```

### <mark style="color:green;">`player.moveTo(position)`</mark>

* <mark style="color:purple;">`position`</mark> - A position as a Vec3 that you want to move to.

Will move to the given position.

**Example:**

```lua
local closestPlayer = engine.getClosestPlayer()

-- Makes you move to closest player
player.moveTo(closestPlayer.origin)
```

### <mark style="color:green;">`player.getName()`</mark>

Returns your name.

**Example:**

```lua
local name = player.getName()
print("My name is " .. name)
```

### <mark style="color:green;">`player.getMainhandWeaponID()`</mark>

Returns the id of your main weapon.

**Example:**

```lua
local mainWeapon = player.getMainhandWeaponID()
player.setCurrentWeaponID(mainWeapon) -- Switch to main weapon
```

### <mark style="color:green;">`player.getRPGWeaponID()`</mark>

Returns the weapon id of your rpg.

**Example:**

```lua
local rpg = player.getRPGWeaponID()
player.setCurrentWeaponID(rpg) -- Switch to rpg
```

### <mark style="color:green;">`player.getWeaponDelay()`</mark>

Returns the delay of your current weapon. The delay indicates how long the weapon is still unusable or in an animation. For example if you shoot the RPG without ADS then it takes time for the RPG to zoom in and eventually fire the rocket. The delay is a number in `miliseconds` how much of that animation is left.

**Example:**

```lua
local delay = player.getWeaponDelay()

-- Turn around right before rpg shoots
if (delay < 20 and player.isUsingRPG()) then
    local view = player.getView()
    view.y = view.y + 180;
    player.setView(view)
end
```

### <mark style="color:green;">`player.isConnectedToServer()`</mark>

Returns true if you're connected to a remote server.

**Example:**

```lua
print("Connected: " .. tostring(player.isConnectedToServer()))
```

### <mark style="color:green;">`player.isOnLocalServer()`</mark>

Returns true if you're on a local server.

**Example:**

```lua
print("on local server?: " .. tostring(player.isOnLocalServer()))
```

### <mark style="color:green;">`player.isSpectating()`</mark>

Returns true if you're spectating.

**Example:**

```lua
print("Spectating?: " .. tostring(player.isSpectating()))
```

### <mark style="color:green;">`player.isUsingRPG()`</mark>

Returns true if you're currently using a RPG.

**Example:**

```lua
-- Disallow rpg usage 
if (player.isUsingRPG()) then
    player.setCurrentWeaponID(player.getMainhandWeaponID())
end
```

### <mark style="color:green;">`player.willLeaveGroundThisFrame()`</mark>

Returns true if you will be considered in air this frame.\
This can either be the result of jumping or walking of a platform etc.

**Example:**

```lua
-- Jump at edges of platforms
if (player.willLeaveGroundThisFrame()) then
    engine.getUserCmd().jump()
end
```

### <mark style="color:green;">`player.willTouchGroundThisFrame()`</mark>

Returns true if you will land on ground this frame.

**Example:**

```lua
if (player.willTouchGroundThisFrame()) then
    print("Landing on ground now")
end
```

### <mark style="color:green;">`player.willTouchGroundNextFrame()`</mark>

Returns true if you will land on ground next frame.

**Example:**

```lua
if (player.willTouchGroundNextFrame()) then
    print("Landing on ground next frame")
end
```

### <mark style="color:green;">`player.wasInAirLastFrame()`</mark>

Returns true if you were in air last frame.

**Example:**

```lua
if (player.wasInAirLastFrame()) then
    print("Was in air last frame (and still could be)")
end
```

### <mark style="color:green;">`player.didTouchGroundLastFrame()`</mark>

Returns true if you landed on ground last frame.hi

{% hint style="info" %}
**Notice** \
This is not returning if you were on ground last frame. \
It is returning if you landed on ground last frame and thus will only be true\
if you were in air 2 frames ago and touched ground last frame.
{% endhint %}

**Example:**

```lua
-- Bhop 
if (player.didTouchGroundLastFrame()) then
    engine.getUserCmd().jump()
end
```

### <mark style="color:green;">`player.willBounceThisFrame()`</mark>

Returns if you will bounce this frame.

**Example:**

```lua
if (player.willBounceThisFrame())
    print("I will bounce now")
end
```

### <mark style="color:green;">`player.willBounceNextFrame()`</mark>

Returns if you will bounce next frame.

**Example:**

```lua
if (player.willBounceNextFrame())
    print("I will bounce next frame")
end
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.anvil.team/namespaces/player.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
