unity键盘控制移动操作(WASD)篇

    今天仔细重新研究了下移动操作,发现input.key那种方法纯属是讨巧操作,实际上它并不好用,在高性能电脑中,我们的电脑每帧有可能会运行上千帧,如果采用这种方法会发生发卡顿,远远没有以下方案丝滑

方案来自unity官网教学视频改写本篇博客内容,原网址如下:

Lesson 2.1 - Player Positioning - Unity Learnhttps://learn.unity.com/tutorial/lesson-2-1-control-player-bounds-with-if-then-statements?uv=2020.3&missionId=5f71fe63edbc2a00200e9de0&pathwayId=5f7e17e1edbc2a5ec21a20af&contentId=5f7229b2edbc2a001f834db7&projectId=5cdcc312edbc2a24a41671e6#首先我们先

1.在场景中建一个模型。

2创建一个脚本,挂载到模型上。

然后再脚本中代码书写如下:

    //声明部分
    public float horizontalinput;//水平参数
    public float Verticalinput;//垂直参数
    float speed=10.0f;//声明一个参数,没有规定

    //在update中书写
    void Update()
    {
        
        horizontalinput = Input.GetAxis("Horizontal");
        //AD方向控制
        Verticalinput = Input.GetAxis("Vertical");
        //WS方向控制
        this.transform.Translate(Vector3.right * horizontalinput * Time.deltaTime * speed);
        控制该物体向侧方移动
        this.transform.Translate(Vector3.forward*  Verticalinput * Time.deltaTime * speed);
        //控制该物体向前后移动
    }

之后保存,你就会发现即使WA、WD、AS、SD一起按也会有移动效果

-------------------------------------------------------

WA、WD、AS、SD一起按也会有移动效果,但是你会发现他们在斜着移动时速度变快了

于是加入以下代码进行限制他们斜着移动的速度

        if (horizontalinput!=0&&Verticalinput!=0)
        {
            horizontalinput = horizontalinput * 0.6f;
            Verticalinput = Verticalinput * 0.6f;
        }

最后完善的代码:

    //声明部分
    public float horizontalinput;//水平参数
    public float Verticalinput;//垂直参数
    float speed=10.0f;//声明一个参数,没有规定

    //在update中书写
    void Update()
    {
        
        horizontalinput = Input.GetAxis("Horizontal");
        //AD方向控制
        Verticalinput = Input.GetAxis("Vertical");

        if (horizontalinput!=0&&Verticalinput!=0)
        {
            horizontalinput = horizontalinput * 0.6f;
            Verticalinput = Verticalinput * 0.6f;
        }
        //WS方向控制
        this.transform.Translate(Vector3.right * horizontalinput * Time.deltaTime * speed);
        控制该物体向侧方移动
        this.transform.Translate(Vector3.forward*  Verticalinput * Time.deltaTime * speed);
        //控制该物体向前后移动
    }

Unity中,你可以使用多种方法来控制物体的移动。其中,常见的方法包括使用刚体(Rigidbody)组件和使用Transform组件中的Translate函数。下面是三种常用的方法: 方法一:使用刚体组件 你可以给游戏物体添加一个刚体组件,然后通过给物体施加力的方式来控制它的运动。具体的代码如下: ```csharp public Rigidbody rd; public float speed; void Start() { speed = 10; rd = GetComponent<Rigidbody>(); } void Update() { if (Input.GetKey(KeyCode.W)) { rd.AddForce(Vector3.forward * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.S)) { rd.AddForce(Vector3.back * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.A)) { rd.AddForce(Vector3.left * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { rd.AddForce(Vector3.right * speed * Time.deltaTime); } } ``` 方法二:使用Transform的Translate函数 你可以使用Transform组件中的Translate函数来控制物体的移动。具体的代码如下: ```csharp public float speed; void Start() { speed = 10; } void Update() { if (Input.GetKey(KeyCode.W)) { transform.Translate(Vector3.forward * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.S)) { transform.Translate(Vector3.back * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.A)) { transform.Translate(Vector3.left * speed * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { transform.Translate(Vector3.right * speed * Time.deltaTime); } } ``` 方法三:使用GetAxis控制移动 你可以使用Input类的GetAxis函数来获取水平和垂直方向上的输入,并使用Translate函数来控制物体的移动。具体代码如下: ```csharp public float speed; private float h, v; void Start() { speed = 5; } void Update() { h = Input.GetAxis("Horizontal"); v = Input.GetAxis("Vertical"); transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime); } ``` 以上是三种常用的Unity键盘控制物体移动的方法。你可以根据自己的需求选择其中一种或多种方法来实现物体的移动。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [unity控制游戏物体移动最基本的三种方法](https://blog.csdn.net/qq_45969040/article/details/109254632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

作孽就得先起床

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值