using UnityEngine;
using System.Collections;
public class Unit : MonoBehaviour {
public static bool isMe = false;
private Vector3 targetPosition;
public float speed = 0.1f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
touchHandler ();
}
void OnMouseDown()
{
isMe = true;
}
void touchHandler(){
if (Input.GetMouseButton (0) && Input.GetMouseButtonDown (0)&&isMe) {
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = 0;
}
if (isMe) {
Vector2 spaceVec2 = new Vector2(targetPosition.x,targetPosition.y) - new Vector2(transform.position.x,transform.position.y);
if(spaceVec2.magnitude<speed)
{
transform.position = targetPosition;
return;
}
spaceVec2.Normalize();
spaceVec2 = spaceVec2*speed;
transform.position += new Vector3(spaceVec2.x,spaceVec2.y,0);
float theSacle = spaceVec2.x/Mathf.Abs(spaceVec2.x);
if(theSacle!= float.NaN)
{
transform.localScale = new Vector3(theSacle,1,1);
}
}
}
void OnCollisionEnter2D(Collision2D coll){
Debug.Log(coll.gameObject.name);
}
}