Sunteți pe pagina 1din 3

using System.

Collections;
using System.Collections.Generic;
using UnityEngine;

public class touchInfoS : MonoBehaviour {


public Vector2 dirVect;
protected float angle;
protected Vector2 touchPoint;
protected bool altarTouch=false;
bool touchDone=false;
private float dist;

public bool isFacingCorrect(Vector2 Direction)


{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float temp=Vector3.Distance (this.gameObject.GetComponent<Collider2D>().bounds.center,
Camera.main.transform.position);
//getting the position of the mouse in 2D space
touchPoint = (Vector2)ray.GetPoint (temp);
//Getting the direction (values range from -1 to 1)
dist = Vector2.Distance (this.gameObject.GetComponent<Collider2D>().bounds.center, touchPoint);
dirVect=(touchPoint-(Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center)/dist;

Vector2 tempDirection = new Vector2 (Direction.x, 0);

float d = Vector2.Dot(tempDirection, dirVect);


//Debug.Log (d);

if (d < -.2f)
{
return false;
}
else
{
touchDone = true;
return true;
}

public void onTouch(bool range, int layerMask=(-1))


{
if (touchDone == false)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float temp=Vector3.Distance (this.gameObject.GetComponent<Collider2D>().bounds.center,
Camera.main.transform.position);
//getting the position of the mouse in 2D space
touchPoint = (Vector2)ray.GetPoint (temp);
//Getting the direction (values range from -1 to 1)
dist = Vector2.Distance (this.gameObject.GetComponent<Collider2D>().bounds.center, touchPoint);
dirVect=(touchPoint-(Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center)/dist;
}

if (layerMask > -1)


{
RaycastHit2D Hit =
Physics2D.Raycast((Vector2)this.gameObject.GetComponent<Collider2D>().bounds.center,
dirVect, dist, layerMask);
// Debug.Log(dist + " " + dirVect);
if (Hit ==true)
{
// Debug.Log(Hit.collider.name);
if (Hit.collider.name == "Altar")
{
Hit.collider.transform.GetComponent<altarS>().selectAltar();
}
else
{
Hit.collider.transform.GetComponentInParent<altarS>().purchase();
}
altarTouch = true;
}
else
{
altarTouch = false;
}
}
else
{
altarTouch = false;
}

//Angle is converted from radians to degrees


angle = ((180 * Mathf.Acos ((float)dirVect.x)) / Mathf.PI);
//range basically allows values past 180
//Debug.Log(dirVect);
if (range == true)
{
if ((dirVect.x < 0 && dirVect.y < 0) || (dirVect.y < 0))
{
//Debug.Log("angle old: " + angle);
angle = 360 - angle;
}
}
//Debug.Log(angle);
touchDone=false;
}

S-ar putea să vă placă și