# SAvatar

**成员** 42

|      |                                                   |                                                                                                                                                                   |
| ---- | ------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 函数 | [AttachTo](#attachto)                             | Attaches the player to the target game object, keeping the avatar fixed in its current position/rotation relative to the target object (e.g. for use in Vehicles) |
| 函数 | [Detach](#detach)                                 | Detaches the avatar from whatever it may be attached to (only works for the player avatar), including chairs, vehicles and so forth.                              |
| 函数 | [OnAvatarAttached](#onavatarattached)             | An event which fires whenever this avatar is attached to a game object in the current scene.                                                                      |
| 函数 | [OnAvatarDetached](#onavatardetached)             | An event which fires whenever this avatar is detached from a game object in the current scene.                                                                    |
| 函数 | [LoadOutfit](#loadoutfit)                         | Only works on a white-label deployment due to abuse concerns.                                                                                                     |
| 函数 | [ResetOutfit](#resetoutfit)                       | Reset current outfit. (white-label only)                                                                                                                          |
| 函数 | [Register](#register)                             | Allow a guest to register. (white-label only)                                                                                                                     |
| 函数 | [ResetOutfitToTemplate](#resetoutfittotemplate)   | Reset current outfit to a specific template. (white-label only)                                                                                                   |
| 函数 | [FindBone](#findbone)                             | Retrieves the specified humanoid bone as an SGameObject.                                                                                                          |
| 函数 | [Teleport](#teleport)                             | Teleport the avatar.                                                                                                                                              |
| 函数 | [SetIKGoal](#setikgoal)                           | Set the IK goal to avatar.                                                                                                                                        |
| 函数 | [ClearIKGoal](#clearikgoal)                       | Clear the IK Goal.                                                                                                                                                |
| 函数 | [PlayCustomAnimation](#playcustomanimation)       | Play a custom animation on the avatar.                                                                                                                            |
| 函数 | [StopCustomAnimation](#stopcustomanimation)       | Make the avatar stops playing custom animation.                                                                                                                   |
| 函数 | [OnAvatarReload](#onavatarreload)                 | This event will be called when the avatar gets loaded.                                                                                                            |
| 函数 | [SynchroniseState](#synchronisestate)             | Synchronise avatar's state to network.                                                                                                                            |
| 函数 | [StartFly](#startfly)                             | Puts the avatar into Fly Mode.                                                                                                                                    |
| 函数 | [EndFly](#endfly)                                 | Exits Avatar out of Fly Mode                                                                                                                                      |
| 函数 | [OnAvatarSkeletonReload](#onavatarskeletonreload) | This event will be called when the avatar skeleton gets loaded.                                                                                                   |
| 函数 | [TeleportToNextEvent](#teleporttonextevent)       | Teleports to the region of the next scheduled event if it starts within the given time window.                                                                    |
| 属性 | [Username](#username)                             | Returns the avatar's current username (note: this can be changed by players for a small fee)                                                                      |
| 属性 | [DisplayName](#displayname)                       | Returns the avatar's display name (falls back to Username if unset).                                                                                              |
| 属性 | [Title](#title)                                   | Returns the avatar's current title                                                                                                                                |
| 属性 | [OrgTitle](#orgtitle)                             | Returns the user's organizational job title (JobTitle).                                                                                                           |
| 属性 | [OrgName](#orgname)                               | Returns the user's organization/company name (Company).                                                                                                           |
| 属性 | [ID](#id)                                         | Returns the avatar's user ID, please note if you store these, this is a 'long' value not a 'int'.                                                                 |
| 属性 | [IsAttached](#isattached)                         | Returns True if avatar is Attached                                                                                                                                |
| 属性 | [MovementSpeed](#movementspeed)                   | How fast the avatar's movement is                                                                                                                                 |
| 属性 | [JumpHeight](#jumpheight)                         | How high the avatar's jump is                                                                                                                                     |
| 属性 | [Guest](#guest)                                   | Is this Avatar a Guest?                                                                                                                                           |
| 属性 | [OutfitID](#outfitid)                             | Returns the current outfit ID of the avatar                                                                                                                       |
| 属性 | [Loaded](#loaded)                                 | Returns true if avatar is Loaded                                                                                                                                  |
| 属性 | [Gender](#gender)                                 | Returns the avatar's gender as a string (e.g., "Male", "Female").                                                                                                 |
| 属性 | [LockObject](#lockobject)                         | The object the Avatar is Attached to.                                                                                                                             |
| 属性 | [Skeleton](#skeleton)                             | Returns a reference to avatar's skeleton GameObject                                                                                                               |
| 属性 | [BlockMovement](#blockmovement)                   | If set to True, avatar's movement will be blocked                                                                                                                 |
| 属性 | [BlockRun](#blockrun)                             | Is the Avatar blocked from running?                                                                                                                               |
| 属性 | [BlockFly](#blockfly)                             | Is the Avatar blocked from flying?                                                                                                                                |
| 属性 | [BlockCrouch](#blockcrouch)                       | Is the Avatar blocked from crouching?                                                                                                                             |
| 属性 | [BlockJump](#blockjump)                           | Is the Avatar blocked from jumping?                                                                                                                               |
| 属性 | [IsGrounded](#isgrounded)                         | Returns True if avatar is Grounded                                                                                                                                |
| 属性 | [GameObject](#gameobject)                         | The reference to the Avatar's GameObject                                                                                                                          |

## 函数

### AttachTo

void AttachTo([SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) target)

Attaches the player to the target game object, keeping the avatar fixed in its current position/rotation relative to the target object (e.g. for use in Vehicles)

参数

| 名称   | 类型                                                                                       | 默认值 |
| ------ | ------------------------------------------------------------------------------------------ | ------ |
| target | [SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) | –      |

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.AttachTo(targetObject)

```

**Lua**

```lua
--the below script makes the player Attach/Detach to/from this object by clicking it
--(Example: Seat/Vehicle/Experience etc.. )

thisGameObject = Space.Host.ExecutingObject
isAttached = 0


OnClick = function()

  if isAttached == 0 then
    Space.Scene.PlayerAvatar.AttachTo(thisGameObject)
    isAttached = 1
  elseif isAttached == 1 then
    Space.Scene.PlayerAvatar.Detach()
    isAttached = 0
  end

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click to Attach/Detach yourself to/from me"
thisGameObject.Clickable.OnClick(OnClick)  

```

### Detach

void Detach()

Detaches the avatar from whatever it may be attached to (only works for the player avatar), including chairs, vehicles and so forth.

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.Detach()

```

**Lua**

```lua
 --the below script makes the player Attach/Detach to/from this object by clicking it
--(Example: Seat/Vehicle/Experience etc.. )

thisGameObject = Space.Host.ExecutingObject
isAttached = 0


OnClick = function()

  if isAttached == 0 then
    Space.Scene.PlayerAvatar.AttachTo(thisGameObject)
    isAttached = 1
  elseif isAttached == 1 then
    Space.Scene.PlayerAvatar.Detach()
    isAttached = 0
  end

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click to Attach/Detach yourself to/from me"
thisGameObject.Clickable.OnClick(OnClick)   

```

### OnAvatarAttached

void OnAvatarAttached()

An event which fires whenever this avatar is attached to a game object in the current scene.

返回值

`void`

示例

**Lua**

```lua
function OAA()
--
end
Space.Scene.PlayerAvatar.OnAvatarAttached(OAA)

```

### OnAvatarDetached

void OnAvatarDetached()

An event which fires whenever this avatar is detached from a game object in the current scene.

返回值

`void`

示例

**Lua**

```lua
function OAD()
--
end
Space.Scene.PlayerAvatar.OnAvatarDetached(OAD)

```

### LoadOutfit

void LoadOutfit(int outfitID)

Only works on a white-label deployment due to abuse concerns. Restrictions: The outfit ID needs to be one of the grid template outfits, with all the restrictions that apply to those (i.e. must use either SW content, or content uploaded by a grid admin account).

参数

| 名称     | 类型 | 默认值 |
| -------- | ---- | ------ |
| outfitID | int  | –      |

返回值

`void`

示例

**Lua**

```lua
local avatar=Space.Scene.PlayerAvatar
avatar.LoadOutfit(10001)

```

**Lua**

```lua
 --the below script changes the player's outfit into 
--a pre-defined outfit if they click the object containing the script
--(Example: Player clicks on a wardrobe and their outfit changes)
--[Only works on white-label deployment]

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.LoadOutfit(2662469)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Put on your favourite suit"
thisGameObject.Clickable.OnClick(OnClick)

```

### ResetOutfit

void ResetOutfit()

Reset current outfit. (white-label only)

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.ResetOutfit()

```

**Lua**

```lua
--Clicking this object will reset player avatar's outfit
thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OnClick = function()
thisPlayer.ResetOutfit() 
end


thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClick)

```

### Register

void Register(string username, string password, string email, bool subscribeToSinespaceNewsletter = false, Closure onSuccess = null, Closure onError = null)

Allow a guest to register. (white-label only)

参数

| 名称                           | 类型    | 默认值 |
| ------------------------------ | ------- | ------ |
| username                       | string  | –      |
| password                       | string  | –      |
| email                          | string  | –      |
| subscribeToSinespaceNewsletter | bool    | false  |
| onSuccess                      | Closure | null   |
| onError                        | Closure | null   |

返回值

`void`

示例

**Lua**

```lua
local avatar=Space.Scene.PlayerAvatar
avatar.Register("MyName","MyPassword","MyEmail",false,function ()
    Space.Log("Success!")
end,function ()
    Space.Log("OnError!!")
end)

```

### ResetOutfitToTemplate

void ResetOutfitToTemplate(int outfitID)

Reset current outfit to a specific template. (white-label only)

参数

| 名称     | 类型 | 默认值 |
| -------- | ---- | ------ |
| outfitID | int  | –      |

返回值

`void`

示例

**Lua**

```lua
local avatar=Space.Scene.PlayerAvatar
avatar.ResetOutfitToTemplate(10001)

```

**Lua**

```lua
--Clicking this object will reset player avatar's outfit to a specific template.
thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OnClick = function()
thisPlayer.ResetOutfitToTemplate(10001)
end


thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClick)

```

### FindBone

[SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) FindBone(string bone)

Retrieves the specified humanoid bone as an SGameObject. Requires a Humanoid rig; returns nil if the bone is missing or the avatar is non‑humanoid. The name is matched case‑insensitively against UnityEngine.HumanBodyBones.

参数

| 参数 | 类型   | 说明                                                                                         |
| ---- | ------ | -------------------------------------------------------------------------------------------- |
| bone | string | Name of a UnityEngine.HumanBodyBones value (case‑insensitive), e.g., "LeftShoulder", "Head". |

返回值

`SGameObject`

示例

**Lua**

```lua
-- Humanoid models only. Pass a UnityEngine.HumanBodyBones name.
local bone = Space.Scene.PlayerAvatar.FindBone("LeftShoulder")
Space.Log(bone.Name) -- Prints the bone name of the corresponding search parameter; on default avatars, prints "LeftShoulder"
--[[
UnityEngine.HumanBodyBones values

Body: Hips, Spine, Chest, UpperChest
Head: Neck, Jaw, Head, LeftEye, RightEye
Left Arm: LeftShoulder, LeftUpperArm, LeftLowerArm, LeftHand
Left Leg: LeftUpperLeg, LeftLowerLeg, LeftFoot, LeftToes
Right Arm: RightShoulder, RightUpperArm, RightLowerArm, RightHand
Right Leg: RightUpperLeg, RightLowerLeg, RightFoot, RightToes
Left Hand (Thumb): LeftThumbProximal, LeftThumbIntermediate, LeftThumbDistal
Left Hand (Index): LeftIndexProximal, LeftIndexIntermediate, LeftIndexDistal
Left Hand (Middle): LeftMiddleProximal, LeftMiddleIntermediate, LeftMiddleDistal
Left Hand (Ring): LeftRingProximal, LeftRingIntermediate, LeftRingDistal
Left Hand (Little): LeftLittleProximal, LeftLittleIntermediate, LeftLittleDistal
Right Hand (Thumb): RightThumbProximal, RightThumbIntermediate, RightThumbDistal
Right Hand (Index): RightIndexProximal, RightIndexIntermediate, RightIndexDistal
Right Hand (Middle): RightMiddleProximal, RightMiddleIntermediate, RightMiddleDistal
Right Hand (Ring): RightRingProximal, RightRingIntermediate, RightRingDistal
Right Hand (Little): RightLittleProximal, RightLittleIntermediate, RightLittleDistal
]]

```

**Lua**

```lua
 --this script will set your avatar's "right hand" as parent of this object
--this way this object will be a child of your right hand and therefore move with it
--(Example: clicking on a gun to equip it)


thisGameObject = Space.Host.ExecutingObject

OnClick = function()

RightHand = Space.Scene.PlayerAvatar.FindBone("RightHand")
thisGameObject.SetParent(RightHand)
thisGameObject.LocalPosition = Vector.New(0,0,0) -- resetting position to match hand

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip="Click to Equip "
thisGameObject.Clickable.OnClick(OnClick) 

```

### Teleport

void Teleport(string landmarkName)

void Teleport([SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) position)

void Teleport([SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) position, [SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md) rotation)

void Teleport(int region)

void Teleport(int region, [SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) position, [SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md) rotation)

Teleport the avatar.

参数

| 名称         | 类型                                                                                       | 默认值 |
| ------------ | ------------------------------------------------------------------------------------------ | ------ |
| landmarkName | string                                                                                     | –      |
| position     | [SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)         | –      |
| rotation     | [SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md) | –      |
| region       | int                                                                                        | –      |

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.Teleport("MyLandMark")
--or
Space.Scene.PlayerAvatar.Teleport( Vector.New(0,0,0) )
--or
Space.Scene.PlayerAvatar.Teleport( Vector.New(0,0,0), Quaternion.New(0,0,0) )
--or
Space.Scene.PlayerAvatar.Teleport( 5432342)
--or
Space.Scene.PlayerAvatar.Teleport( 5432342, Vector.New(0,0,0), Quaternion.New(0,0,0) )

```

**Lua**

```lua
--the below script teleports player to a specific landmark
--if they click the object containing it
--(Example: A region promotional/advertisement sign)
--[You need a landmark in the scene with name "Fashion Section Landmark"

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport("Fashion Section Landmark")
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to go to the fashion section!"
thisGameObject.Clickable.OnClick(OnClick)

```

### SetIKGoal

void SetIKGoal(string goal, bool positionGoal, float positionStrength, [SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md) positionTarget, bool rotationGoal, float rotationStrength, [SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md) rotationTarget)

Set the IK goal to avatar.

参数

| 名称             | 类型                                                                                       | 默认值 |
| ---------------- | ------------------------------------------------------------------------------------------ | ------ |
| goal             | string                                                                                     | –      |
| positionGoal     | bool                                                                                       | –      |
| positionStrength | float                                                                                      | –      |
| positionTarget   | [SVector](/docs/zh-Hans/scripting/client-scripting-api-reference/types/svector.md)         | –      |
| rotationGoal     | bool                                                                                       | –      |
| rotationStrength | float                                                                                      | –      |
| rotationTarget   | [SQuaternion](/docs/zh-Hans/scripting/client-scripting-api-reference/types/squaternion.md) | –      |

返回值

`void`

示例

**Lua**

```lua
avatar=Space.Scene.PlayerAvatar
pos=avatar.FindBone("Chest").WorldPosition
avatar.SetIKGoal("Chest",false,0,pos,true,20,Quaternion.Euler(0,60,0))

```

### ClearIKGoal

void ClearIKGoal(string goal)

Clear the IK Goal.

参数

| 名称 | 类型   | 默认值 |
| ---- | ------ | ------ |
| goal | string | –      |

返回值

`void`

示例

**Lua**

```lua
avatar=Space.Scene.PlayerAvatar
pos=avatar.FindBone("Chest").WorldPosition
avatar.SetIKGoal("Chest",false,0,pos,true,20,Quaternion.Euler(0,60,0))
Space.Host.InvokeDelayed(function ()
    avatar.ClearIKGoal("Chest")
end,5)
-- will clear after 5 seconds.

```

### PlayCustomAnimation

void PlayCustomAnimation([SResource](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sresource.md) animationClip)

Play a custom animation on the avatar.

参数

| 名称          | 类型                                                                                   | 默认值 |
| ------------- | -------------------------------------------------------------------------------------- | ------ |
| animationClip | [SResource](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sresource.md) | –      |

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.PlayCustomAnimation(Space.Resources[1])

```

**Lua**

```lua
 --the below script makes the player Play/Stop a custom animation when clicking this object
--(Example: Dance Machine )
--[Required: Place an animation in the first "Resource" in the scripting runtime]

thisGameObject = Space.Host.ExecutingObject
isDancing = 0


OnClick = function()

  if isDancing == 0 then
    Space.Scene.PlayerAvatar.PlayCustomAnimation(Space.Resources[1])
    isDancing = 1
  elseif isDancing == 1 then
    Space.Scene.PlayerAvatar.StopCustomAnimation()
    isDancing = 0
  end

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click to Start/Stop dancing"
thisGameObject.Clickable.OnClick(OnClick)   

```

### StopCustomAnimation

void StopCustomAnimation()

Make the avatar stops playing custom animation.

返回值

`void`

示例

**Lua**

```lua
avatar=Space.Scene.PlayerAvatar
avatar.PlayCustomAnimation(Space.Resources[1])
avatar.StopCustomAnimation()

```

**Lua**

```lua
 --the below script makes the player Play/Stop a custom animation when clicking this object
--(Example: Dance Machine )
--[Required: Place an animation in the first "Resource" in the scripting runtime]

thisGameObject = Space.Host.ExecutingObject
isDancing = 0


OnClick = function()

  if isDancing == 0 then
    Space.Scene.PlayerAvatar.PlayCustomAnimation(Space.Resources[1])
    isDancing = 1
  elseif isDancing == 1 then
    Space.Scene.PlayerAvatar.StopCustomAnimation()
    isDancing = 0
  end

end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click to Start/Stop dancing"
thisGameObject.Clickable.OnClick(OnClick)   

```

### OnAvatarReload

void OnAvatarReload(Closure o)

This event will be called when the avatar gets loaded.

参数

| 参数 | 类型               | 说明                                                                       |
| ---- | ------------------ | -------------------------------------------------------------------------- |
| o    | Closure (callback) | A function that will be called once the OnAvatarReload event is triggered. |

返回值

`void`

示例

**Lua**

```lua
local avatar=Space.Scene.PlayerAvatar
avatar.OnAvatarReload(function ()
    Space.Log(avatar.OutfitID)
end)

```

**Lua**

```lua
--this script ensures the avatar is still animating after their avatar gets reloaded
thisPlayer = Space.Scene.PlayerAvatar
theAnimation = Space.GetResource("The Animation") --add animation to resources in Scripting Runtime
wasAnimated = true --you'll need to set this according to the situation, its true now for example's sake


ResetDance = function()
 if wasAnimated then
  thisPlayer.PlayCustomAnimation(theAnimation)
 end

end
thisPlayer.OnAvatarReload(ResetDance)

```

### SynchroniseState

void SynchroniseState()

Synchronise avatar's state to network.

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.SynchroniseState()

```

### StartFly

void StartFly()

Puts the avatar into Fly Mode.

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.StartFly()

```

**Lua**

```lua
--the below script teleports the player to a sky location after he clicks
--but then immediately puts the character in fly mode so they don't fall back down
--(Example: Sky exhibit)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(Vector.New(0,10,0))
Space.Scene.PlayerAvatar.StartFly()
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to view our sky exhibit!"
thisGameObject.Clickable.OnClick(OnClick) 

```

### EndFly

void EndFly()

Exits Avatar out of Fly Mode

返回值

`void`

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.EndFly()

```

**Lua**

```lua
--the below script teleports a player flying in the sky back to the ground
--and then ends fly mode by switching to Idle so they remain standing
--(Example: Going back down to ground from Sky exhibit)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
Space.Scene.PlayerAvatar.Teleport(Vector.New(0,0,0))
Space.Scene.PlayerAvatar.EndFly()
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Click me to go back to the ground!"
thisGameObject.Clickable.OnClick(OnClick) 

```

### OnAvatarSkeletonReload

void OnAvatarSkeletonReload(Closure o)

This event will be called when the avatar skeleton gets loaded.

参数

| 参数 | 类型               | 说明                                                                               |
| ---- | ------------------ | ---------------------------------------------------------------------------------- |
| o    | Closure (callback) | A function that will be called once the OnAvatarSkeletonReload event is triggered. |

返回值

`void`

示例

**Lua**

```lua
avatar=Space.Scene.PlayerAvatar
Space.Log(avatar.BlockJump)
avatar.OnAvatarSkeletonReload(function ()
    Space.Log(avatar.OutfitID)
end)

```

**Lua**

```lua
--this script ensures the avatar is still animating after their avatar's skeleton gets reloaded
thisPlayer = Space.Scene.PlayerAvatar
theAnimation = Space.GetResource("The Animation") --add animation to resources in Scripting Runtime
wasAnimated = true --you'll need to set this according to the situation, its true now for example's sake


ResetDance = function()
 if wasAnimated then
  thisPlayer.PlayCustomAnimation(theAnimation)
 end

end
thisPlayer.OnAvatarSkeletonReload(ResetDance)

```

### TeleportToNextEvent

double TeleportToNextEvent(long withinSeconds = 600)

Teleports to the region of the next scheduled event if it starts within the given time window. Returns 0 if a teleport occurs; returns the number of seconds until the next event if it is outside the cutoff; returns -1 if there are no upcoming events.

参数

| 参数          | 类型 | 说明                                                                  |
| ------------- | ---- | --------------------------------------------------------------------- |
| withinSeconds | long | Maximum seconds ahead to consider for auto-teleport. Defaults to 600. |

返回值

`double`

示例

**Lua**

```lua
timeUntilNextEvent = Space.Scene.PlayerAvatar.TeleportToNextEvent()
--or
timeUntilNextEvent = Space.Scene.PlayerAvatar.TeleportToNextEvent(500)

```

## 属性

### Username

string Username {get; }

Returns the avatar's current username (note: this can be changed by players for a small fee)

示例

**Lua**

```lua
playerUsername = Space.Scene.PlayerAvatar.Username

```

**Lua**

```lua
 --this script, once clicked, will get all avatars in the scene
--and print their Username(including self)
--(Example: scoreboards/radars/scanners/game machines)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
allAvatars = Space.Scene.AllAvatars

        for var=1, #allAvatars do
    local Username = allAvatars[var].Username
     Space.Log("Username: " .. Username)
        end

end

 
thisGameObject.AddClickable() 
thisGameObject.Clickable.Tooltip = "Click to print all avatars' Username"
thisGameObject.Clickable.OnClick(OnClick) 

```

### DisplayName

string DisplayName {get; }

Returns the avatar's display name (falls back to Username if unset).

示例

**Lua**

```lua
playerDisplayName =  Space.Scene.PlayerAvatar.DisplayName

```

### Title

string Title {get; }

Returns the avatar's current title

示例

**Lua**

```lua
playerTitle = Space.Scene.PlayerAvatar.Title

```

**Lua**

```lua
 --this script, once clicked, will get all avatars in the scene
--and print their Title and Username(including self)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
allAvatars = Space.Scene.AllAvatars

        for var=1, #allAvatars do
    local Username = allAvatars[var].Username
    local Title = allAvatars[var].Title
     Space.Log(Title .. " " .. Username)
        end

end

 
thisGameObject.AddClickable() 
thisGameObject.Clickable.Tooltip = "Click to print all avatars' Titles and Username"
thisGameObject.Clickable.OnClick(OnClick)

```

### OrgTitle

string OrgTitle {get; }

Returns the user's organizational job title (JobTitle).

示例

**Lua**

```lua
playerJobTitle = Space.Scene.PlayerAvatar.OrgTitle

```

### OrgName

string OrgName {get; }

Returns the user's organization/company name (Company).

示例

**Lua**

```lua
playerCompanyName = Space.Scene.PlayerAvatar.OrgName

```

### ID

long ID {get; }

Returns the avatar's user ID, please note if you store these, this is a 'long' value not a 'int'.

示例

**Lua**

```lua
playerID = Space.Scene.PlayerAvatar.ID

```

**Lua**

```lua
--this script, once clicked, will get all avatars in the scene
--and print their ID(including self)
--(Example: scoreboards/radars/scanners/game machines)

thisGameObject = Space.Host.ExecutingObject

OnClick = function()
allAvatars = Space.Scene.AllAvatars

        for var=1, #allAvatars do
    local ID = allAvatars[var].ID
     Space.Log("ID: " .. ID)
        end

end

 
thisGameObject.AddClickable() 
thisGameObject.Clickable.Tooltip = "Click to print all avatars' ID"
thisGameObject.Clickable.OnClick(OnClick) 

```

### IsAttached

bool IsAttached {get; }

Returns True if avatar is Attached

示例

**Lua**

```lua
isAttached = Space.Scene.PlayerAvatar.IsAttached

```

### MovementSpeed

float MovementSpeed {get; set; }

How fast the avatar's movement is

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.MovementSpeed = 20

```

**Lua**

```lua
 --the below script makes the object give the player triple movement speed
--once it's clicked, and then returns them to original speed after 10 seconds
--(Example: Triple Speed Powerup (avatar running race))

thisGameObject = Space.Host.ExecutingObject

TripleSpeedCoroutine = function()
local originalSpeed = Space.Scene.PlayerAvatar.MovementSpeed
Space.Scene.PlayerAvatar.MovementSpeed = originalSpeed * 3
coroutine.yield(10)
Space.Scene.PlayerAvatar.MovementSpeed = originalSpeed
  
end


OnClick = function()
Space.Host.StartCoroutine(TripleSpeedCoroutine)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Triple Speed Powerup!"
thisGameObject.Clickable.OnClick(OnClick)  

```

### JumpHeight

float JumpHeight {get; set; }

How high the avatar's jump is

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.JumpHeight = 20

```

**Lua**

```lua
 --the below script makes the object give the player triple jump height
--once it's clicked, and then returns them to original jump height after 10 seconds
--(Example: Triple Jump Height Powerup )

thisGameObject = Space.Host.ExecutingObject
originalJumpHeight = Space.Scene.PlayerAvatar.JumpHeight

TripleJumpHeightCoroutine = function()
Space.Scene.PlayerAvatar.JumpHeight = originalJumpHeight * 3
coroutine.yield(10)
Space.Scene.PlayerAvatar.JumpHeight = originalJumpHeight
end


OnClick = function()
Space.Host.StartCoroutine(TripleJumpHeightCoroutine)
end

thisGameObject.AddClickable()
thisGameObject.Clickable.Tooltip = "Triple JumpHeight Powerup!"
thisGameObject.Clickable.OnClick(OnClick)  

```

### Guest

bool Guest {get; }

Is this Avatar a Guest?

示例

**Lua**

```lua
isGuest = Space.Scene.PlayerAvatar.Guest

```

**Lua**

```lua
--this script will make this object non-clickable for guest players

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OnClick = function()
Space.Log("Do Something")
end

if not thisPlayer.Guest then --if player is not guest
thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClick)
end

```

### OutfitID

long OutfitID {get; }

Returns the current outfit ID of the avatar

示例

**Lua**

```lua
avOutfit = Space.Scene.PlayerAvatar.OutfitID

```

### Loaded

bool Loaded {get; }

Returns true if avatar is Loaded

示例

**Lua**

```lua
isLoaded = Space.Scene.PlayerAvatar.Loaded

```

**Lua**

```lua
--this script will wait for a joining avatar to load first before making it play an animation
--(otherwise that animation may be reset if played before load is complete)

NewJoinAnimation = Space.GetResource("The Animation")

OnPlayerJoin = function(NewAvatar)
  
     Space.Host.StartCoroutine(function()
      
      while not NewAvatar.Loaded do coroutine.yield(0) end --makes it wait
 Space.Log("yes")     
      NewAvatar.PlayCustomAnimation(NewJoinAnimation) --at this point Loaded is True
      end)
      

end


Space.Scene.OnPlayerJoin(OnPlayerJoin)

```

### Gender

string Gender {get; }

Returns the avatar's gender as a string (e.g., "Male", "Female").

示例

**Lua**

```lua
playerGender = Space.Scene.PlayerAvatar.Gender

```

### LockObject

[SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) LockObject {get; }

The object the Avatar is [*Attached*](/docs/zh-Hans/scripting/client-scripting-api-reference/types/savatar.md#attachto) to. Returns **Null** if Avatar is not attached.

示例

**Lua**

```lua
lockObject = Space.Scene.PlayerAvatar.LockObject

```

### Skeleton

[SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) Skeleton {get; }

Returns a reference to avatar's skeleton GameObject

示例

**Lua**

```lua
avSkeleton = Space.Scene.PlayerAvatar.Skeleton

```

**Lua**

```lua
--clicking this object will replace your avatar's animator controller (found on the skeleton)
thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

customController = Space.GetResource("animcont")


OnClick = function()
 thisPlayer.Skeleton.Animator.Controller = customController
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClick)

```

### BlockMovement

bool BlockMovement {get; set; }

If set to True, avatar's movement will be blocked

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.BlockMovement = true

```

**Lua**

```lua
 --the below script makes this object a boundary area where any user entering it
--will be blocked from movement for 20 seconds, then the object will release controls and
--destroy itself after that
--(Example: freezing trap)
--[This object's collider needs to be set as IsTrigger (Please read about OnTriggerStart for troubleshooting)]

thisGameObject = Space.Host.ExecutingObject
FrozenTime = 0.0

OnTriggerStart = function(TriggerStarter)
  if TriggerStarter.Root.Avatar ~= nil then
    if TriggerStarter.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      Space.Scene.PlayerAvatar.BlockMovement = true
    end
  end
end

OnTriggerStay = function(TriggerStayer)
  if TriggerStayer.Root.Avatar ~= nil then
    if TriggerStayer.Root.Avatar.Username == Space.Scene.PlayerAvatar.Username then
      FrozenTime = FrozenTime + Space.DeltaTime
        if FrozenTime > 20 then
        Space.Scene.PlayerAvatar.BlockMovement = false
        thisGameObject.Destroy()
        end
    end
  end
end

thisGameObject.SubscribeToEvents()
thisGameObject.OnTriggerStart(OnTriggerStart)
thisGameObject.OnTriggerStay(OnTriggerStay)

```

### BlockRun

bool BlockRun {get; set; }

Is the Avatar blocked from running?

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.BlockRun = true

```

**Lua**

```lua
--this script will block a player's run ability when entering trigger collider and release it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 
    thisPlayer.BlockRun = true
  end
end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then
   thisPlayer.BlockRun = false
  end 
end

thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)

```

### BlockFly

bool BlockFly {get; set; }

Is the Avatar blocked from flying?

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.BlockFly = true

```

**Lua**

```lua
--this script will block a player's fly ability when entering trigger collider and release it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 
    thisPlayer.BlockFly = true
  end
end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then
   thisPlayer.BlockFly = false
  end 
end

thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)

```

### BlockCrouch

bool BlockCrouch {get; set; }

Is the Avatar blocked from crouching?

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.BlockCrouch = true

```

**Lua**

```lua
--this script will block a player's crouch ability when entering trigger collider and release it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 
    thisPlayer.BlockCrouch = true
  end
end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then
   thisPlayer.BlockCrouch = false
  end 
end

thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)

```

### BlockJump

bool BlockJump {get; set; }

Is the Avatar blocked from jumping?

示例

**Lua**

```lua
Space.Scene.PlayerAvatar.BlockJump = true

```

**Lua**

```lua
--this script will block a player's jump ability when entering trigger collider and release it when leaving
--[Requires this object to have a "Trigger" collider]

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar

OTS = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then 
    thisPlayer.BlockJump= true
  end
end

OTE = function(gameObject)
  if gameObject.Avatar ~= nil and thisPlayer == gameObject.Avatar then
   thisPlayer.BlockJump= false
  end 
end

thisObject.OnTriggerStart(OTS)
thisObject.OnTriggerExit(OTE)

```

### IsGrounded

bool IsGrounded {get; }

Returns True if avatar is Grounded

示例

**Lua**

```lua
isGrounded = Space.Scene.PlayerAvatar.IsGrounded

```

### GameObject

[SGameObject](/docs/zh-Hans/scripting/client-scripting-api-reference/types/sgameobject.md) GameObject {get; }

The reference to the Avatar's GameObject

示例

**Lua**

```lua
playerObject = Space.Scene.PlayerAvatar.GameObject

```

**Lua**

```lua
--Clicking this object will teleport you 2 units forward and make you face the opposite way

thisObject = Space.Host.ExecutingObject
thisPlayer = Space.Scene.PlayerAvatar


OnClick = function()
local targetPos = thisPlayer.GameObject.WorldPosition + (thisPlayer.GameObject.Forward * 2) 
local targetRot = thisPlayer.GameObject.WorldRotation  *  Quaternion.AngleAxis(Vector.Up, 180)
thisPlayer.Teleport(targetPos, targetRot)
  
  
end

thisObject.AddClickable()
thisObject.Clickable.OnClick(OnClick)

```
