using UnityEngine; using System.Collections; using System.Collections.Generic; public enum CLIENT_MOVE_TYPE { Loop, PingPong, } public class ClientMove : MonoBehaviour { public float _Speed; public List _MovePoses; public CLIENT_MOVE_TYPE _MoveType; public bool _MoveInverval = false; public float _MotionRot = 0; public void Start() { _TargetPosIdx = 0; _Closic = true; if (_MovePoses.Count < 3) { _MoveType = CLIENT_MOVE_TYPE.Loop; } transform.position = _MovePoses[_TargetPosIdx]; GetNextPos(); } public void Update() { MoveUpdate(); } #region move private int _TargetPosIdx = 0; private bool _Closic = true; public void SetSpeed(float speedValue) { _Speed = speedValue; } public void GetNextPos() { switch (_MoveType) { case CLIENT_MOVE_TYPE.Loop: ++_TargetPosIdx; if (_TargetPosIdx == _MovePoses.Count) _TargetPosIdx = 0; break; case CLIENT_MOVE_TYPE.PingPong: if (_Closic) { ++_TargetPosIdx; if (_TargetPosIdx == _MovePoses.Count) { _TargetPosIdx = _MovePoses.Count - 2; _Closic = false; } } else { --_TargetPosIdx; if (_TargetPosIdx == -1) { _TargetPosIdx = 1; _Closic = true; } } break; } } public void MoveUpdate() { if (_Speed == 0) return; transform.LookAt(_MovePoses[_TargetPosIdx]); if (_MotionRot != 0) { transform.rotation = Quaternion.Euler(transform.eulerAngles + new Vector3(0, _MotionRot, 0)); } var speed = (_MovePoses[_TargetPosIdx] - transform.position).normalized * _Speed; var distance = Vector3.Distance(_MovePoses[_TargetPosIdx], transform.position); var moveDistance = _Speed * Time.deltaTime; if (moveDistance > distance) { transform.position = _MovePoses[_TargetPosIdx]; GetNextPos(); if (_MoveInverval) { _Speed = 0; } } else { transform.position += speed * Time.deltaTime; } } #endregion }