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() { _deltaTime = Time.deltaTime * simulationSpeed; if (isSimulating) { //Check edges we are in contact with. Check for changes MovementData newMovement = new MovementData(movement); //Private Vector2 inputForce = GameManager.inputAxis * moveSpeed * _deltaTime; newMovement.velocity += inputForce; collisionSides = GetSurroundings(newMovement.position); CollisionModify(collisionSides, ref newMovement); if (collisionSides.HasFlag(Edges.Below)) { newMovement.velocity.x = Mathf.MoveTowards(newMovement.velocity.x, 0f, groundDrag); } MovementData prediction = Verlet(newMovement, collisionSides.HasFlag(Edges.Below) ? Vector2.zero : Acceleration()); //Private //NOTE this implementation for collisions is NOT accurate with Verlet!! Need a better solution... float distance = Vector2.Distance(movement.position, prediction.position); Vector2 direction = VectorExtras.Direction(movement.position, prediction.position); List <RaycastHit2D> hits = FilterIgnored(ignoredColliders, Physics2D.BoxCastAll(movement.position, collider.size, 0f, direction, distance)); if (hits.Count > 0) //Did we hit something? { RaycastHit2D hit = hits[0]; prediction.position = hit.centroid + (hit.normal * bloatSpacing); } movement = prediction; transform.position = new Vector3(movement.position.x, movement.position.y, 0f); collisionSides = GetSurroundings(transform.position); ExternalCall(collisionSides); } }
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); }