public IEnumerator MoveToWayPoint(int currActionNum) { int savedActionNum = currActionNum; movementList[savedActionNum].isCoroutine = true; moveInfo.currWayPoint = 0; while (savedActionNum == currActionNum) { Movement currMoveThisAct = movementList[savedActionNum]; int currWayIndex = moveInfo.currWayPoint; if (currWayIndex < currMoveThisAct.wayPointList.Count) { Movement.WayPoint currWayPoint = currMoveThisAct.wayPointList[currWayIndex]; if (currWayPoint.startDelay != 0) { rgBody.velocity = Vector3.zero; yield return(new WaitForSeconds(currWayPoint.startDelay)); currWayPoint.startDelay = 0; } moveInfo.target = currWayPoint.targetTrans.position; moveInfo.moveDirection = moveInfo.target - transform.position; moveInfo.velocity = rgBody.velocity; if (moveInfo.moveDirection.magnitude < 0.5f) { moveInfo.currWayPoint++; } else { moveInfo.velocity = moveInfo.moveDirection.normalized * currWayPoint.speed; } } else { if (currMoveThisAct.isRepeat) { moveInfo.currWayPoint = 0; } else { moveInfo.velocity = Vector3.zero; } } rgBody.velocity = moveInfo.velocity; yield return(null); } rgBody.velocity = Vector3.zero; movementList[savedActionNum].isCoroutine = false; }
public void SetMovement(Transform spawnPos, Movement movement) { if (movement.type == Movement.Type.WAY_POINT) { spawnPosTrans = spawnPos; if (movementList.Count == 0) { movementList.Add(new Movement()); Movement currMovement = movementList[0]; currMovement.type = Movement.Type.WAY_POINT; currMovement.wayPointParent = movement.wayPointParent; currMovement.constantSpeed = movement.constantSpeed; currMovement.isRepeat = movement.isRepeat; currMovement.isConstantSpeed = movement.isConstantSpeed; for (int i = 0; i < movement.wayPointParent.childCount; i++) { if (currMovement.wayPointList.Count - 1 < i) { currMovement.wayPointList.Add(new Movement.WayPoint()); } Movement.WayPoint currWaypoint = movementList[0].wayPointList[i]; currWaypoint.targetTrans = movement.wayPointParent.GetChild(i); currWaypoint.speed = movement.wayPointList[i].speed; currWaypoint.startDelay = movement.wayPointList[i].startDelay; if (currMovement.isConstantSpeed) { currWaypoint.speed = movement.constantSpeed; } } } } else if (movement.type == Movement.Type.SINE_WAVE) { if (movementList.Count == 0) { movementList.Add(new Movement()); Movement currMovement = movementList[0]; currMovement.type = Movement.Type.SINE_WAVE; currMovement.sinewave.direction = movement.sinewave.direction; currMovement.sinewave.speed = movement.sinewave.speed; currMovement.sinewave.frequency = movement.sinewave.frequency; currMovement.sinewave.magnitude = movement.sinewave.magnitude; currMovement.sinewave.magExpandMult = movement.sinewave.magExpandMult; currMovement.sinewave.isStartLeft = movement.sinewave.isStartLeft; currMovement.sinewave.startPos = (Vector2)spawnPos.position; currMovement.sinewave.curveAxis = GetCurveAxis(currMovement.sinewave.direction, currMovement.sinewave.isStartLeft); mPrevPos = transform.position; } } else if (movement.type == Movement.Type.IN_AND_OUT) { movementList.Add(new Movement()); Movement currMovement = movementList[0]; currMovement.type = Movement.Type.IN_AND_OUT; currMovement.inOut.direction = movement.inOut.direction; currMovement.inOut.speed = movement.inOut.speed; currMovement.inOut.time = movement.inOut.time; currMovement.inOut.waitBeforeGoingOut = movement.inOut.waitBeforeGoingOut; } }