using System.Collections; using System.Collections.Generic; using UnityEngine; public class MonsterLogic : MonoBehaviour { [Tooltip("怪物前進速度")] public float MonsterZSpeed = -5.0f; [Tooltip("怪物左右切換時間間隔")] public float MonsterMoveInterval = 2.0f; //怪物左右橫移速度= 10.0f; float MonsterXSpeed = 1.0f; void Start() { InvokeRepeating("moveXRandom", 2.0f, MonsterMoveInterval); } void Update() { float dx = MonsterXSpeed * Time.deltaTime; float dz = MonsterZSpeed * Time.deltaTime; this.transform.Translate(dx, 0, dz, Space.Self); } void moveXRandom() { float[] xSpeed = { -10, 5, -5, 10 }; int xindex = Random.Range(0, 4); MonsterXSpeed = xSpeed[xindex]; } }