若想在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中改變移動速度了