using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { public float speed; public Vector3 moveOffset; private Vector3 targetPos; private Vector3 startPos; // Start is called before the first frame update void Start() { startPos = transform.position; targetPos = startPos; } // Update is called once per frame void Update() { transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime); if(transform.position == targetPos) { if(targetPos == startPos) { targetPos = startPos + moveOffset; } else { targetPos = startPos; } } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter