iT邦幫忙

2025 iThome 鐵人賽

0
自我挑戰組

從零開始做出我的第一款 Unity 遊戲系列 第 12

DAY12-讓角色動起來(3)

  • 分享至 

  • xImage
  •  

若想在unity編輯器中直接調物件移動速度的話,我們可以將程式碼改成這樣:

using UnityEngine;

public class Snack : MonoBehaviour
{
    Vector3 direction;
    public float speed;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        Debug.Log(transform.position);

        Time.timeScale = speed;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("W");
            direction = Vector3.up;
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            Debug.Log("A");
            direction = Vector3.left;
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("S");
            direction = Vector3.down;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("D");
            direction = Vector3.right;
        }


    }
    void FixedUpdate()
    {
        transform.Translate(direction);

    }
}

這樣就可以在物件的inspector中改變移動速度了
https://ithelp.ithome.com.tw/upload/images/20251020/20178853ZQbrvoYwJi.png


上一篇
DAY11-讓角色動起來(2)
下一篇
DAY13-讓貪吃蛇吃東西吧!
系列文
從零開始做出我的第一款 Unity 遊戲21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言