Enemy patrol using DOTween in Unity
Enemy patrol using DOTween in Unity
CODE:
public class enemydotwen : MonoBehaviour
{
[SerializeField]public Transform[] target;
[SerializeField] float speed ;
[SerializeField] int i;
bool facingRight=false;
void Start()
{
move(i);
}
void move(int point)
{
this.transform.DOMove( this.target[point].position,this.speed)
.SetEase(Ease.InOutSine)
.OnPlay(() =>{
this.FlipEnemy(this.target[point]);
})
.OnComplete(() =>{
if(this.i< this.target.Length-1)
this.i +=1;
else
this.i =0;
this.move(i);
})
;
}
void FlipEnemy(Transform point)
{
if(this.transform.position.x < point.position.x && facingRight)
{
flip();
}
else if(this.transform.position.x > point.position.x && !facingRight)
{
flip();
}
}
void flip()
{
Vector3 thescale = this.transform.localScale;
thescale.x *= -1;
this.transform.localScale = thescale;
this.facingRight = !this.facingRight;
}
}
Post a Comment