public override void OnActivateStay(HedgehogController controller) { if (controller == null) { return; } var index = Controllers.IndexOf(controller); if (index < 0) { return; } ControllerProgress[index] += TravelSpeed / _cachedLength * Time.fixedDeltaTime; var walk = Physics2DUtility.Walk(_cachedPath, ControllerProgress[index], false) + (Vector2)(controller.transform.position - controller.Sensors.Center.position); Velocities[index] = (walk - (Vector2)controller.transform.position) / Time.fixedDeltaTime; controller.transform.position = new Vector3(walk.x, walk.y, controller.transform.position.z); if (ControllerProgress[index] > 1.0f) { ObjectTrigger.Deactivate(controller); } }
/// <summary> /// Updates the platform's path with data from the collider stored in Path. /// </summary> public void ReconstructPath() { _previousPath = Path; Path.enabled = !DisablePathCollider; _cachedPath = Physics2DUtility.GetPoints(Path); _cachedLength = Physics2DUtility.GetPathLength(_cachedPath); }
// State behaviour protected override void LaunchProjectile() { Collider2D enemy = Physics2DUtility.FindClosestTarget(user, preset.SearchRange, preset.HitboxLayer.Character); CharacterMover mover = user.GetComponent <CharacterMover>(); // TODO: Can cache this if you need to // Float //user.GetComponent<CharacterCore>().ApplyFloat(floatDuration, floatDuration); // Initialise projectile Vector2 direction = user.transform.right * preset.ProjectileSpeed; if (enemy != null) { direction = (enemy.transform.position - user.transform.position).normalized * preset.ProjectileSpeed; // Change facing direction of user if (enemy.transform.position.x > user.transform.position.x) // If enemy is to the right of user, face right { mover.FaceRight(true); } else { mover.FaceRight(false); } } InstantiateProjectile(direction, preset.ProjectileDuration).transform.right = direction; }
public void Move(Vector2 direction) { if (direction.x != 0 && !isJumpInCooldown && Physics2DUtility.IsOnGround(transform.position, myCollider)) { myRigidbody.AddForce(new Vector2(1 * Mathf.Sign(direction.x), 1) * jumpForce, ForceMode2D.Impulse); isJumpInCooldown = true; Invoke("EnableJump", jumpCooldown); } }
void Update() { var xVelocity = myRigidbody.velocity.x; if (Math.Abs(xVelocity) > VelocityFlipTrigger) { // reverse object var newScale = transform.localScale; if (Mathf.Sign(newScale.x) != Mathf.Sign(xVelocity)) { newScale.x *= -1; } transform.localScale = newScale; } animator.SetBool("isOnGround", Physics2DUtility.IsOnGround(transform.position, myCollider)); }
public static Collider2D ComputeHorizontalRaycast(Vector2 origin, Collider2D refCollider, int objectLayer, bool drawDebug, float debugDuration, Color color) { origin.y -= refCollider.bounds.size.y * 0.55f - refCollider.offset.y; origin.x -= refCollider.bounds.size.x * 0.3f; float distance = refCollider.bounds.size.x * 0.8f; int layer = CollisionMatrixLayerMasks.MaskForLayer(objectLayer); if (drawDebug) { Debug.DrawRay(origin, Vector3.right * distance, color, debugDuration); } RaycastHit2D hitInfos = Physics2DUtility.RaycastAllWithEffector(origin, Vector2.right, distance, layer, drawDebug); return(hitInfos ? hitInfos.collider : null); }
private bool WrapWireAroundObstacles(float2 a, float2 b, int insertAt) { var added = false; var offset = new float2(0, 0.075f); var hit = Physics2D.Linecast(a + offset, b + offset, GroundMask); if (hit) { float2 point = hit.point; var foundValid = false; if (hit.collider is PolygonCollider2D polygon) { point = Physics2DUtility.GetClosestPointFromPoint(hit.point, polygon); foundValid = true; } else if (hit.collider is CompositeCollider2D composite) { try { point = Physics2DUtility.GetClosestPointFromPoint(hit.point, composite); foundValid = true; } catch { foundValid = false; } } if (foundValid && NotTooSimilarToLast(point)) { Placed.Insert(insertAt, new Point { Value = point }); if (DrawGizmos) { DebugDraw.Sphere(new float3(point, 0), 0.15f, Color.red, 3f); } added = true; } } if (DrawGizmos) { Debug.DrawLine(new float3(a + offset, 0), new float3(b + offset, 0), Color.blue); } return(added); }
void FixedUpdate() { bool isOnGround = Physics2DUtility.IsOnGround(transform.position, myCollider); bool isAffectedByGravity = myRigidbody.gravityScale > 0; // Jump take-off if (startJump && (isOnGround || !isAffectedByGravity) && !isOnCooldown) { Vector2 jumpDirection, finalJumpForce; if (isAffectedByGravity) { jumpDirection = new Vector2(direction.x, 1); finalJumpForce = jumpForce; } else { jumpDirection = direction; if (jumpDirection == Vector2.zero) { jumpDirection = Vector2.up; } finalJumpForce = new Vector2(jumpForce.y, jumpForce.y); } myRigidbody.AddForce(jumpDirection * finalJumpForce, ForceMode2D.Impulse); OnJump?.Invoke(); isOnCooldown = true; Invoke(nameof(EnableJump), cooldownTime); } // Jump maneuvers if (!isOnGround && isAffectedByGravity) { myRigidbody.AddForce(Vector2.right * direction.x * lateralForce); if (extendJump && myRigidbody.velocity.y > 0) { myRigidbody.AddForce(Vector2.up * extendForce); } } startJump = false; extendJump = false; }
public static Collider2D ComputeVerticalRayWithEffector(Vector2 origin, Collider2D refCollider, float forward, int objectLayer, bool drawDebug = false, float debugDuration = 0f) { float distance = refCollider.bounds.size.y * 0.75f; origin.x += forward * refCollider.bounds.size.x * 0.55f + (forward * 0.1f); origin.y += refCollider.bounds.size.y * 0.4f + refCollider.offset.y; int layer = CollisionMatrixLayerMasks.MaskForLayer(objectLayer); if (drawDebug) { Debug.DrawRay(origin, Vector3.down * distance, Color.red, debugDuration); } RaycastHit2D hitInfos = Physics2DUtility.RaycastAllWithEffector(origin, Vector2.down, distance, layer, drawDebug); return(hitInfos ? hitInfos.collider : null); }
// TODO: Fix animation error when leaving object void Update() { var inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")); if (inputDirection.x != 0) { // reverse object var newScale = transform.localScale; if (Mathf.Sign(newScale.x) != Mathf.Sign(inputDirection.x)) { newScale.x *= -1; } transform.localScale = newScale; //animator.SetTrigger("move"); } else { animator.SetTrigger("stop"); } animator.SetBool("isOnGround", Physics2DUtility.IsOnGround(transform.position, myCollider)); animator.SetFloat("verticalSpeed", myRigidbody.velocity.y); }
/// <summary> /// Moves the platform to a point on the path. /// </summary> /// <param name="t">A point on the path, 0 being its start point and 1 being its end point.</param> public override void To(float t) { transform.position = _cachedPath == null ? Physics2DUtility.Walk(Path, t) : Physics2DUtility.Walk(_cachedPath, t); }