首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无输入的Unity字符漫游

无输入的Unity字符漫游
EN

Stack Overflow用户
提问于 2021-08-03 19:17:05
回答 1查看 76关注 0票数 1

问题:我的玩家就像我总是拿着“w”键一样。

所以我试着使用第一人称一体机以及简单的fps播放器控制器。我使用输入系统管理器和可视化工具仔细检查了输入系统,拔掉了除键盘和鼠标之外的所有usb设备,没有发现任何异常输入被检测到。即使没有插入usb设备,播放器也可以行走。

因此,多个项目文件中的多个播放器预置,无论是urp还是3d,即使我拔下了每个usb设备,也会像检测到前向行走输入一样工作。我不知所措

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-08-04 04:34:56

确保没有将速度设置为始终增加。使其仅在按下“w”键时增加。这是我用来移动球员的代码-

代码语言:javascript
复制
//Input
float x, y;
bool jumping, sprinting, crouching;

//Movement
public float moveSpeed = 4500;
public float maxSpeed = 20;


private void Update()
{
    MyInput();
}

private void MyInput()
{
    x = Input.GetAxisRaw("Horizontal");
    y = Input.GetAxisRaw("Vertical");
}

private void Movement()
{
    //Extra gravity
    rb.AddForce(Vector3.down * Time.deltaTime * 10);

    Vector2 mag = FindVelRelativeToLook();
    float xMag = mag.x, yMag = mag.y;

    CounterMovement(x, y, mag);

    //Set max speed
    float maxSpeed = this.maxSpeed;

    //If speed is larger than maxspeed, cancel out the input so you don't go over max speed
    if (x > 0 && xMag > maxSpeed) x = 0;
    if (x < 0 && xMag < -maxSpeed) x = 0;
    if (y > 0 && yMag > maxSpeed) y = 0;
    if (y < 0 && yMag < -maxSpeed) y = 0;

    //Some multipliers
    float multiplier = 1f, multiplierV = 1f;

    // Movement in air
    if (!grounded)
    {
        multiplier = 0.3f;
        multiplierV = 0.3f;
    }

    //Apply forces to move player
    rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
    rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
}

希望这会对你有所帮助。

谢谢。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68641698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档