void Update() { if (positions == null || positions.Count <= 0 || segments == null || segments.Count <= 0) { return; } positions[0] = VectorExtras.AnchoredMovePosTowardTarget(head.transform.position, positions[0], segmentSpacingMax); float ang = VectorExtras.VectorToDegrees(VectorExtras.Direction(segments[0].transform.position, head.transform.position)); angles[0] = Mathf.MoveTowardsAngle(angles[0], ang, bodyRotateSpeed * Time.deltaTime); segments[0].transform.rotation = Quaternion.AngleAxis(angles[0], Vector3.forward); segments[0].transform.position = positions[0]; Vector3 nextPos = VectorExtras.AnchoredMovePosTowardTarget(head.transform.position, positions[0], segmentSpacingMax); for (int i = 1; i < segments.Count; i++) { positions[i] = VectorExtras.AnchoredMovePosTowardTarget(nextPos, positions[i], segmentSpacingMax / 3f); ang = VectorExtras.VectorToDegrees(VectorExtras.Direction(segments[i].transform.position, nextPos)); angles[i] = Mathf.MoveTowardsAngle(angles[i], ang, bodyRotateSpeed * Time.deltaTime); segments[i].transform.rotation = Quaternion.AngleAxis(angles[i], Vector3.forward); segments[i].transform.position = positions[i]; nextPos = VectorExtras.AnchoredMovePosTowardTarget(positions[i - 1], positions[i], segmentSpacingMax / 3f); } }
void Update() { if (initalized == false) { return; } int index = TerrainController.Singleton.ClosestSegmentIndexToPoint(head.transform.position); index = Mathf.Min(index + 1, TerrainController.Singleton.path.Count - 1); Vector3 target; if (Vector3.Distance(head.position, PlayerController.Singleton.transform.position) < chasePlayerDistance) { target = PlayerController.Singleton.transform.position; } else { target = TerrainController.Singleton.path[index] + (Vector2)TerrainController.Singleton.transform.position; } float myDistance = TerrainController.Singleton.PointDistanceAlongPathTotal(head.position); float playerDistance = TerrainController.Singleton.PointDistanceAlongPathTotal(PlayerController.Singleton.transform.position); distToPlayer = playerDistance - myDistance; float percent = VectorExtras.ReverseLerp(distToPlayer, minSpeedDistance, maxSpeedDistance); float curve = speedCurve.Evaluate(percent) * (maxSpeed - minSpeed); speed = curve + minSpeed; //VectorExtras.Remap( minSpeedDistance, maxSpeedDistance, minSpeed, maxSpeed, diff ); head.position = Vector3.MoveTowards(head.position, target, speed * Time.deltaTime); float a = VectorExtras.VectorToDegrees(VectorExtras.Direction(head.position, target)); headAngle = Mathf.MoveTowardsAngle(headAngle, a, headRotateSpeed * Time.deltaTime); head.transform.rotation = Quaternion.AngleAxis(headAngle, Vector3.forward); Debug.DrawLine(head.position, target, Color.cyan); }