public float MoveTo(Vector3 target, RectangleBound rectangleBound = null) { // no movement if pushed if (isPushed) { return(0); } float directionX = 0; bool stopMoving = false; if (MoveUtils.TargetReachedXY(transform, target, speed, smoothTime)) { // Target already reached stopMoving = true; } // Check if actor ist still in bounds if (!stopMoving && rectangleBound != null && !rectangleBound.IsInBoundX(transform.position)) { stopMoving = true; } if (stopMoving) { return(0); } else if (target != Vector3.positiveInfinity) { if (transform.position.x > target.x) { directionX = -1; } if (transform.position.x < target.x) { directionX = 1; } } // perform moving action //targetPosition = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); targetPosition = target - transform.position; targetPosition = targetPosition / targetPosition.magnitude * speed; if (moveSinus) { targetPosition = targetPosition + Vector3.up * (Mathf.Sin(Time.timeSinceLevelLoad * frequency) * magnitude); } return(directionX); }
void Update() { if (!stopMovement) { if (!vector && MoveUtils.TargetReachedXY(transform, target, speed, 0)) { // if target reached but nothing hit, destroy object Destroy(gameObject); } if (useTrail) { if (Time.timeSinceLevelLoad >= lastInterval + trailInterval) { InstantiateEffect(trailPrefab, false); lastInterval = Time.timeSinceLevelLoad; } } if (vector) { rigidb.MovePosition(transform.position + (targetPosition * speed * Time.deltaTime)); //transform.position += targetPosition * speed * Time.deltaTime; } else { if (moveInArc) { // Compute the next position, with arc added in float x0 = startPosition.x; float x1 = target.x; float dist = x1 - x0; float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime); float baseY = Mathf.Lerp(startPosition.y, target.y, (nextX - x0) / dist); float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist); targetPosition = new Vector3(nextX, baseY + arc, transform.position.z); rigidb.MovePosition(targetPosition); } else { Vector3 newPos = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); rigidb.MovePosition(Utils.MakePixelPerfect(newPos)); } } } }