private void InstantiateAndRotate(Transform rotateToThisObject, GameObject gameObjectToRotate, out Transform knife) { GameObject objectToRotate = Instantiate(gameObjectToRotate, wheelCollider.ClosestPoint(rotateToThisObject.position), Quaternion.identity); knife = objectToRotate.transform; Debug.Log(objectToRotate); Vector2 direction = wheelCollider.ClosestPoint(rotateToThisObject.position) - (Vector2)wheelCollider.transform.position; float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90; objectToRotate.GetComponent <Rigidbody2D>().rotation = angle; }
void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "enemy1" || col.gameObject.tag == "enemy2") { gameObject.GetComponent <BoxCollider2D>().isTrigger = true; gameObject.GetComponent <CircleCollider2D>().isTrigger = true; Movement.is_Grounded = false; gameObject.GetComponent <Rigidbody2D>().velocity = Vector2.zero; Movement.can = false; } if (col.gameObject.tag == "ground") { foreach (ContactPoint2D contact in col.contacts) { Vector2 v = contact.point; CircleCollider2D cc2 = gameObject.GetComponent <CircleCollider2D>(); Vector2 close = cc2.ClosestPoint(v); float dis = Vector2.Distance(v, close); if (dis < ep) { Movement.is_Grounded = true; Movement.num_Of_Jumps = 0; } } } }
private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.layer == 16) { Instantiate(lightningEffect, collider.ClosestPoint(collision.transform.position), Quaternion.identity); } }
bool doesOrbOverlap(CircleCollider2D c, Vector3 point) { Vector3 closest = c.ClosestPoint(point); // Because closest=point if point is inside - not clear from docs I feel return(closest == point); }
public override void Hit(Vector3 playerPosition, Vector3 playerVelocity) { FMODUnity.RuntimeManager.PlayOneShot(DestroyEvent, transform.position); Vector2 closestPointV2 = collider.ClosestPoint(playerPosition); Vector3 closestPoint = new Vector3(closestPointV2.x, closestPointV2.y, 0); Vector3 furthestPoint = closestPoint + 2 * (transform.position - closestPoint); Vector3 goldDirection = (transform.position - playerPosition).normalized; Rigidbody2D coinBody = Instantiate(coinPrefab, furthestPoint, Quaternion.identity); coinBody.velocity = goldDirection * Vector3.Dot(playerVelocity, goldDirection); GameObject.Destroy(gameObject); }
void IDragHandler.OnDrag(PointerEventData eventData) { Vector2 delta = eventData.position - pointerStartPosition; transform.position = startingPosition + delta; if (!circle.OverlapPoint(transform.position)) { transform.position = circle.ClosestPoint(transform.position); } axis = delta / circle.radius; }
void OnCollisionStay2D(Collision2D col) { foreach (ContactPoint2D contact in col.contacts) { Vector2 v = contact.point; CircleCollider2D cc2 = gameObject.GetComponent <CircleCollider2D>(); Vector2 close = cc2.ClosestPoint(v); float dis = Vector2.Distance(v, close); if (dis < ep) { Movement.is_Grounded = true; Movement.num_Of_Jumps = 0; } } }
private ShipController CreateShip(PlanetController target, int damage) { Vector3 targetPos; Vector3 instantiatePoint; targetPos = target.transform.position; instantiatePoint = planetCollider.ClosestPoint(targetPos); ShipController newShip; newShip = Instantiate(shipPrefab); newShip.transform.position = instantiatePoint; newShip.Init(OwnerID, target, damage); return(newShip); }
private GameObject GetClosestVertexToPoint(GameObject[] vertices, Vector2 point) { GameObject closestVertex = null; float closestDistance = float.MaxValue; foreach (GameObject vertex in vertices) { CircleCollider2D collider = vertex.GetComponentInChildren <CircleCollider2D>(); float currentDistance = (collider.ClosestPoint(point) - point).magnitude; if (currentDistance < closestDistance) { closestDistance = currentDistance; closestVertex = vertex; } } return(closestDistance < MAX_VERTEX_PROXIMITY ? closestVertex : null); }
void OnMouseDown() { if (!first) { if (turnController.GetComponent <Info>().player_turn == owner) { Debug.Log("Seleccionado" + this.gameObject); first = this.transform.gameObject; } } else if (first == this.transform.gameObject) { Debug.Log("Ya no hay first" + this.gameObject); Debug.Log("Todos los nodos eliminados" + this.gameObject); for (int i = 0; i < total_nodes; i++) { if (objectives[i] != null) { int pointsToAdd = (int)(100f * Vector2.Distance(transform.position, objectives[i].transform.position) / Camera.main.GetComponent <CameraSize>().camWidth); if (points + pointsToAdd >= 100) { points = 100; } else { points += pointsToAdd; } } objectives[i] = null; try { Destroy(unions[i].gameObject); } catch { } unions[i] = null; } used_nodes = 0; first = null; } else if (first != this.transform.gameObject) { Seleccion_y_Union first_code = first.GetComponent <Seleccion_y_Union>(); //con este if, considero que quede al menos un "objective" en null if (first_code.used_nodes + 1 <= first_code.total_nodes) { //veo la union, si ya existe, la destruyo for (int i = 0; i < first_code.total_nodes; i++) { if (first_code.objectives[i] == this.gameObject) { //ya existe, elimino la flecha y libero el cupo first_code.used_nodes -= 1; int pointsToAdd = (int)(100f * Vector2.Distance(gameObject.transform.position, first_code.transform.position) / Camera.main.GetComponent <CameraSize>().camWidth); if (first_code.points + pointsToAdd >= 100) { first_code.points = 100; } else { first_code.points += pointsToAdd; } first_code.objectives[i] = null; Destroy(first_code.unions[i].gameObject); first_code.unions[i] = null; //Debug.Log("nodo sacado" + this.gameObject); first = null; return; } } int index_to_use = 0; //no existe, selecciono el espacio para agregar la union for (int i = 0; i < first_code.total_nodes; i++) { // cuando encontremos uno vacio lo usaremos if (first_code.objectives[i] == null) { first_code.objectives[i] = this.gameObject; index_to_use = i; first_code.used_nodes += 1; break; } } //finalmente, casteo la linea //Debug.Log(first.transform.position); float distTotal = Vector2.Distance(first.transform.position, transform.position); float distX = Math.Abs(first.transform.position.x - transform.position.x); float distY = Math.Abs(first.transform.position.y - transform.position.y); float middleX; float middleY; Vector2 colliderClosest1 = collider.ClosestPoint(first.transform.position); Vector2 colliderClosest2 = first.GetComponent <CircleCollider2D>().ClosestPoint(transform.position); float colliderDist = Vector2.Distance(colliderClosest1, colliderClosest2); //calculos de puntos medios #region middleX = (first.transform.position.x + transform.position.x) / 2f; middleY = (first.transform.position.y + transform.position.y) / 2f; #endregion int angle; angle = (int)(Math.Atan(distY / distX) * 180 / Math.PI); //fixing angle depending on direction if (first.transform.position.x < transform.position.x && first.transform.position.y >= transform.position.y) { angle *= -1; } else if (first.transform.position.x >= transform.position.x && first.transform.position.y >= transform.position.y) { angle += 180; } else if (first.transform.position.x >= transform.position.x && first.transform.position.y < transform.position.y) { angle += (90 - angle) * 2; } Debug.Log("angulo: " + angle); if ((int)(100f * distTotal / Camera.main.GetComponent <CameraSize>().camWidth) <= first.GetComponent <Seleccion_y_Union>().points) { GameObject g = Instantiate(arrow, new Vector3(middleX, middleY, transform.position.z), Quaternion.identity); g.transform.Rotate(0, 0, angle - 90); g.transform.localScale = new Vector3(0.3f, 0.15f * colliderDist, 1); first_code.unions[index_to_use] = g; Debug.Log("Union entre" + first + "and" + this.gameObject); first.GetComponent <Seleccion_y_Union>().points -= (int)(100f * distTotal / Camera.main.GetComponent <CameraSize>().camWidth); first = null; } else { //no se completo la union por distancia, por lo tanto se libera el nodo sendMessage("Too far!"); Debug.Log("Union entre" + first + "and" + this.gameObject + "no creada, mucha distancia."); first_code.objectives[index_to_use] = null; first_code.used_nodes -= 1; first = null; } } else { for (int i = 0; i < first_code.total_nodes; i++) { if (first_code.objectives[i] == this.gameObject) { //ya existe, elimino la flecha y libero el cupo first_code.used_nodes -= 1; first_code.objectives[i] = null; Destroy(first_code.unions[i].gameObject); first_code.unions[i] = null; Debug.Log("nodo sacado" + this.gameObject); first = null; return; } else { //Debug.Log("This node canĀ“t have more nodes"); sendMessage("No connections left!"); } } } } }