> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/playcanvas-plugin/first-person-view.md).

# 第一人称视角

第一人称视角——Convai 集成的 PlayCanvas 插件指南。

### 为平面添加物理组件

1. 放大平面并为其添加物理组件。添加碰撞和刚体组件。
2. 导入 Ammo Js（启用物理功能）。根据平面的尺寸调整碰撞组件的大小。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FGjJz0gy52pnJ5CKzw2ym%2FScreenshot%20(21).png?alt=media&amp;token=f36b7f54-3dd7-46ca-a93d-11f5f06556cb" alt=""><figcaption><p>平面上的物理组件</p></figcaption></figure>

### 玩家刚体

1. **创建新实体**
   * 在 PlayCanvas 编辑器中，右键单击层级面板，然后选择“创建新实体”。
2. **添加物理组件**
   * 选中新实体后，单击编辑器右上角的“添加组件”按钮。
   * 搜索“Physics”，然后为该实体添加“Physics”组件。
3. **添加胶囊碰撞组件**
   * 保持选中该实体，再次单击“添加组件”。
   * 搜索“Collision”，然后为该实体添加“Collision Capsule”组件。
4. **调整实体的 Y 轴位置**
   * 调整“Translation”中的“Y”值，将实体定位在平面上方。
5. **添加刚体组件**
   * 保持选中该实体，再次单击“添加组件”。
   * 搜索“Rigidbody”，然后为该实体添加“Rigidbody”组件。
   * 将刚体的“Type”设为“Dynamic”。
6. **调整角因子**
   * 在 Rigidbody 组件中找到“Angular Factor”部分。
   * 将 Angular Factor 的“X”、“Y”和“Z”值设为 0。

<figure><img src="https://2402152281-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEtUJA212Zc1S9ACc8T4l%2Fuploads%2FiDVjhJv3eFTNanhYVO9W%2FScreenshot%20(22).png?alt=media&amp;token=c91c5599-bf59-43c5-b0e0-7f56ddd02589" alt=""><figcaption><p>玩家组件的物理组件</p></figcaption></figure>

### 第一人称视角的摄像机控制

创建一个 FirstPersonView\.js 脚本，并添加以下代码。有关摄像机控制的实现示例，请参阅 [PlayCanvas 文档](https://api.playcanvas.com/) 以及示例。将此脚本附加到玩家胶囊体上。

```javascript
var FirstPersonMovement = pc.createScript('firstPersonMovement');
var fpsCamera = null
var overChat = false
FirstPersonMovement.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: '调整玩家移动速度'
});

FirstPersonMovement.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.07,
    description: '调整视角移动的灵敏度'
});
// 每个实体仅调用一次的初始化代码
FirstPersonMovement.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();

    var app = this.app;
    app.mouse.on("mousemove", this._onMouseMove, this);
    app.mouse.on("mousedown", function () { 
        if(!overChat){
            app.mouse.enablePointerLock();
        }   
    },this);
    // 检查必需的组件
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
};

// 每帧调用的更新代码
FirstPersonMovement.prototype.update = function(dt) {
// 如果未在编辑器中指定摄像机，则创建一个
    if (!this.camera) {
        this._createCamera();
    }

    var force = this.force;
    var app = this.app;

    fpsCamera = this.camera;
    var forward = this.camera.forward;
    var right = this.camera.right;

    // 移动 
    var x = 0;
    var z = 0;

    const convaiFormEl = document.getElementById("convai-input");
    // 使用 w-a-s-d 键
    if(app.keyboard.isPressed(pc.KEY_A) && !(document.activeElement === convaiFormEl)){
         x -= right.x;
         z -= right.z;
    }

      if (app.keyboard.isPressed(pc.KEY_D) && !(document.activeElement === convaiFormEl)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W) && !(document.activeElement === convaiFormEl)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S) && !(document.activeElement === convaiFormEl)) {
        x -= forward.x;
        z -= forward.z;
    }

    // 根据按键输入的方向对角色施加力
    if (x !== 0 || z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // 根据鼠标事件更新摄像机角度
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};

FirstPersonMovement.prototype._onMouseMove = function (e) {
    // 如果指针已解锁
    // 如果按下鼠标左键，则根据鼠标移动更新摄像机
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }

    const convaiChat = document.getElementById("convai-chat")
    convaiChat.addEventListener("mouseover",()=>{
    overChat = true;
    })

    convaiChat.addEventListener("mouseout",()=>{
    overChat = false;
    })
};

FirstPersonMovement.prototype._createCamera = function () {
    // 如果用户尚未指定摄像机，则创建一个新的摄像机
    this.camera = new pc.Entity();
    this.camera.setName("第一人称摄像机");
    this.camera.addComponent("camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/playcanvas-plugin/first-person-view.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
