1
Fork 0
mirror of https://github.com/Steffo99/gravity-fusion.git synced 2024-11-24 17:24:19 +00:00
gravity-fusion/Assets/Components/PushOnMouseClick.cs

34 lines
1.2 KiB
C#
Raw Permalink Normal View History

2019-10-06 15:44:41 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PushOnMouseClick : MonoBehaviour
{
2019-10-07 11:44:26 +00:00
public int mouseButton = 1;
public float pushForce = 0;
public float pushRadius = 0;
2019-10-07 14:23:15 +00:00
2019-10-07 13:11:17 +00:00
protected GameController gameController;
protected void Awake() {
gameController = GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>();
}
2019-10-06 15:44:41 +00:00
protected Vector3 GetWorldMousePosition() {
return Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
2019-10-07 11:44:26 +00:00
protected void Update()
2019-10-06 15:44:41 +00:00
{
2019-10-07 14:23:15 +00:00
if(!gameController.upgradePanel.gameObject.activeSelf && Input.GetMouseButton(mouseButton)) {
2019-10-06 15:44:41 +00:00
Vector3 mousePosition = GetWorldMousePosition();
Collider2D[] affected = Physics2D.OverlapCircleAll(mousePosition, pushRadius);
foreach(Collider2D collider in affected) {
float distance = Vector3.Distance(mousePosition, collider.transform.position);
Vector2 direction = (collider.transform.position - mousePosition).normalized;
2019-10-07 14:23:15 +00:00
collider.attachedRigidbody?.AddForce(direction * pushForce / distance);
2019-10-06 15:44:41 +00:00
}
}
}
}