[Unity2d] 怪物左右走程式碼

Unity2d 怪物左右走程式碼

using UnityEngine;
using System.Collections;

public class BadMob : MonoBehaviour {

    int speed = 5;
    bool whatWay = true
    int timer = 0;
    
    // Update is called once per frame
    void Update () {
        timer++;
        if (timer >= 40) {
            timer =0;
            if(whatWay)
            {
                whatWay = false;
                return;
            }
            else{
                whatWay = true;
                return;
            }
        }

        if (whatWay) {
            //right
            Vector3 temp = transform.localScale;
            temp.x =2;
            transform.localScaletemp;
            transform.Translate((new Vector3(speed,0,0)) * Time.deltaTime);

        } else {
            //left
            Vector3 temp = transform.localScale;
            temp.x =-2;
            transform.localScaletemp;
            transform.Translate(Vector3.forward * Time.deltaTime);
            transform.Translate((new Vector3(-speed,0,0)) * Time.deltaTime);
        }
    }

}