108 lines
3.5 KiB
C#
108 lines
3.5 KiB
C#
using UnityEngine;
|
|
|
|
[AddComponentMenu("Destructible 2D/D2D Drag To Shoot")]
|
|
public class D2D_DragToShoot_obi : MonoBehaviour
|
|
{
|
|
public GameObject Bullet;
|
|
|
|
public float Power = 3.0f;
|
|
|
|
public float AngleOffset;
|
|
|
|
public float AngleRandomness;
|
|
|
|
public SpriteRenderer Indicator;
|
|
|
|
private bool down;
|
|
|
|
public GameObject startFirePosition = null;
|
|
|
|
// Use this for initialization
|
|
void Start()
|
|
{
|
|
if (Indicator != null)
|
|
Indicator = Instantiate(Indicator);
|
|
}
|
|
|
|
protected virtual void Update()
|
|
{
|
|
// End dragging?
|
|
if (Input.GetMouseButton(0) == false && down == true)
|
|
{
|
|
down = false;
|
|
|
|
// Fire?
|
|
if (Camera.main != null && Bullet != null)
|
|
{
|
|
var endMousePosition = Input.mousePosition;
|
|
//var startPos = Camera.main.ScreenToWorldPoint(startMousePosition);
|
|
var startPos = startFirePosition.transform.position;
|
|
var endPos = Camera.main.ScreenToWorldPoint(endMousePosition);
|
|
var vec = endPos - startPos;
|
|
var angle = D2D_Helper.Atan2(vec) * -Mathf.Rad2Deg + AngleOffset + Random.Range(-0.5f, 0.5f) * AngleRandomness;
|
|
var clone = D2D_Helper.CloneGameObject(Bullet, null, startPos, Quaternion.Euler(0.0f, 0.0f, angle));
|
|
var cloneRb2D = clone.GetComponent<Rigidbody2D>();
|
|
|
|
if (cloneRb2D != null)
|
|
{
|
|
cloneRb2D.velocity = (startPos - endPos) * Power;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Show dragging?
|
|
if (Indicator != null)
|
|
{
|
|
Indicator.enabled = down;
|
|
|
|
if (Camera.main != null && down == true)
|
|
{
|
|
var currentMousePosition = Input.mousePosition;
|
|
//var startPos = Camera.main.ScreenToWorldPoint( startMousePosition);
|
|
var startPos = startFirePosition.transform.position;
|
|
var currentPos = Camera.main.ScreenToWorldPoint(currentMousePosition);
|
|
var scale = Vector3.Distance(currentPos, startPos);
|
|
var angle = D2D_Helper.Atan2(currentPos - startPos) * Mathf.Rad2Deg;
|
|
|
|
//Indicator.transform.position = Camera.main.ScreenToWorldPoint(startMousePosition);
|
|
Indicator.transform.position = startFirePosition.transform.position;
|
|
Indicator.transform.localRotation = Quaternion.Euler(180.0f, 0.0f, angle);
|
|
Indicator.transform.localScale = new Vector3(scale, scale, scale);
|
|
}
|
|
}
|
|
}
|
|
|
|
void OnMouseDown()
|
|
{
|
|
if (Input.GetMouseButton(0) == true && startFirePosition != null)
|
|
{
|
|
down = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마우스가 내려간 오브젝트를 가지고 옵니다.
|
|
/// </summary>
|
|
/// <returns>선택된 오브젝트</returns>
|
|
private GameObject GetClickedObject()
|
|
{
|
|
//충돌이 감지된 영역
|
|
RaycastHit hit;
|
|
//찾은 오브젝트
|
|
GameObject target = null;
|
|
|
|
//마우스 포이트 근처 좌표를 만든다.
|
|
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
|
|
|
//마우스 근처에 오브젝트가 있는지 확인
|
|
if (true == (Physics.Raycast(ray.origin, ray.direction * 10, out hit)))
|
|
{
|
|
//있다!
|
|
|
|
//있으면 오브젝트를 저장한다.
|
|
target = hit.collider.gameObject;
|
|
}
|
|
|
|
return target;
|
|
}
|
|
} |