protected virtual void CollisionAvoidance(Collider2D other) { //Debug.Log("Collision Avoidance between " + name + " and: " + other.name); var distance = circleCollider.Distance(other); //var force = distance.normal * distance.distance * maxForce; //Debug.DrawRay(transform.position, force, Color.blue, 0.2f); //AddForce(force); Vector2 closestPointOther = distance.pointB; Vector2 position = transform.position; var toObstacle = closestPointOther - position; var projection = Vector2.Dot(toObstacle, transform.up); var scalingFactor = projection * (1 / Mathf.Max(toObstacle.magnitude, 0.1f)); Vector2 steeringCA = -transform.up * scalingFactor * maxForce; steeringCA = Vector2.ClampMagnitude(steeringCA, maxForce); AddForce(steeringCA, maxForce / 2); Debug.DrawRay(transform.position, steeringCA, Color.blue, 0.2f); var brakingFactor = 0.1f * Mathf.Max(rigidBody.velocity.sqrMagnitude, 0.1f); Vector2 brakingForce = -1 * brakingFactor * transform.right; // The braking force is clamped higher than the steering and turning forces. brakingForce = Vector2.ClampMagnitude(brakingForce, 2 * maxForce); AddForce(brakingForce); Debug.DrawRay(transform.position, brakingForce, Color.cyan, 0.2f); }
// OnTriggerStay2D is called once per frame for every Collider2D other that is touching the trigger (2D physics only) private void OnTriggerStay2D(Collider2D collision) { if (collision.transform.tag == "Shot") { float distance = circleCollider.Distance(collision).distance; float NormalizeDistance = Mathf.Abs(distance / circleCollider.radius); collision.transform.parent.GetComponent <ShotController>().LocalSpeedModifier(NormalizeDistance); } }
private void FindClosestHookable() { autoAimPosition.Variable.Value = transform.position; hasTarget = false; if ((Vector2)transform.localPosition == Vector2.zero) { return; } Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, aimHelpRadius, LayerList.Hook.LayerMask); if (colliders.Length >= 1) { for (int i = 0; i < colliders.Length; i++) { Aimable aimable = colliders[i].GetComponent <Aimable>(); if (aimable != null) { if (!hasTarget) { closestColliderDistance2D = circleCollider2D.Distance(colliders[i]); if (closestColliderDistance2D.isValid) { autoAimPosition.Variable.Value = closestColliderDistance2D.pointB; hasTarget = true; } } else { ColliderDistance2D newColliderDistance2D = circleCollider2D.Distance(colliders[i]); if (newColliderDistance2D.isValid && newColliderDistance2D.distance < closestColliderDistance2D.distance) { closestColliderDistance2D = newColliderDistance2D; autoAimPosition.Variable.Value = closestColliderDistance2D.pointB; } } } } } }
void Update() { if (Input.GetKeyDown(KeyCode.Return)) { if (colliderBouton.Distance(colliderJoueur).distance < 0.10) { if (Logique.jouable) { switch (this.name) { case ("BoutonNewEnigme"): processBoutonNewEnigme(); break; case ("BoutonDivise7"): processBoutonDivise7(); break; case ("BoutonDivise10"): processBoutonDivise10();; break; case ("BoutonSetClef"): processSetClef(); break; case ("BoutonSwap"): processSwap(); break; case ("BoutonGetDifficulty"): processGetDifficulty(); break; case ("BoutonTryDoor"): processTryDoor(); break; } } } } }
private float DistanceToCollider(Vector2 _targetPosition, Collider2D _buttonCollider) { if (_buttonCollider != null) { CircleCollider2D closestPointCollider = closestPointGameObject.AddComponent <CircleCollider2D>(); closestPointCollider.radius = blobRadius; closestPointGameObject.transform.position = Camera.main.ScreenToWorldPoint(_targetPosition); ColliderDistance2D distanceCollider = closestPointCollider.Distance(_buttonCollider); Destroy(closestPointCollider); float buttonDistance = Mathf.Abs(distanceCollider.distance) / maxDistance - minDistance; float distance = snapFrom.Evaluate(buttonDistance + (1 - wobbleActive)); return(distance); } else { return(0); } }