コード例 #1
0
 void OnTapEvent(TapEvent evt)
 {
     if (evt.target != null)
     {
         MeshClick mesh_click = evt.target.gameObject.GetComponent <MeshClick>();
         if (mesh_click != null)
         {
             mesh_click.OnMeshClick(evt.target.gameObject);
         }
     }
 }
コード例 #2
0
    private bool FindTargetAndLocation(TouchWrapper touch, out Transform target, out Vector3 location, out Vector3 groundPosition, out Vector3 direction)
    {
        target = null;

        if (Camera.main == null)
        {
            location       = default(Vector3);
            groundPosition = default(Vector3);
            direction      = default(Vector3);
            return(false);
        }
        if (mMainCamera == null)
        {
            mMainCamera = Camera.main;
        }

        Ray        ray = mMainCamera.ScreenPointToRay(new Vector3(Mathf.Clamp(touch.position.x, 0, Screen.width), Mathf.Clamp(touch.position.y, 0, Screen.height), 0.0f));
        RaycastHit rayHit;

        groundPosition = default(Vector3);
        direction      = default(Vector3);
        location       = default(Vector3);

        if (Physics.Raycast(ray, out rayHit, 200, _groundLayer))
        {
            groundPosition = rayHit.point;
        }

        //Check raycast first for best precision
        if (Physics.Raycast(ray, out rayHit, 200, _characterLayers | _groundLayer))
        {
            if (PlayerManager.IsPlayer(rayHit.transform.gameObject))
            {
                target = rayHit.transform;
            }
            else if (rayHit.transform.gameObject.layer != LayerMask.NameToLayer("Ground"))
            {
                Selectable selectable = rayHit.transform.GetComponent <Selectable>();
                if (selectable == null)
                {
                    //If raycast couldn't find a selectable widen to an overlap sphere cast and pick closest selectable
                    Collider[] colliders = Physics.OverlapSphere(rayHit.point, fingerRadius, 1 << LayerMask.NameToLayer("Enemy"));
                    if (colliders.Length > 0)
                    {
                        float    minSqDistance   = float.MaxValue;
                        Collider closestCollider = colliders[0];
                        foreach (Collider collider in colliders)
                        {
                            float sqrDistance = Vector3.SqrMagnitude(collider.transform.position - rayHit.point);
                            if (sqrDistance < minSqDistance)
                            {
                                minSqDistance   = sqrDistance;
                                closestCollider = collider;
                            }
                        }
                        selectable = closestCollider.gameObject.GetComponent <Selectable>();
                    }
                }

                //Move or target selectable
                if (selectable != null)
                {
                    target = selectable.transform;
                }
                else
                {
                    target = null;
                }
            }

            //if the object has a ServerCallback then call it!!
            if (rayHit.transform.gameObject != null)
            {
                MeshClick callback = rayHit.transform.gameObject.GetComponent <MeshClick>();
                if (callback != null)
                {
                    target = callback.transform;
                }
            }


            if (target != null)
            {
                location = target.transform.position;
            }
            else
            {
                location = groundPosition;
            }

            if (_playerTransform != null && _muzzleTransform != null)
            {
                Vector3 muzzleYOffset = new Vector3(0, _muzzleTransform.position.y - _playerTransform.position.y, 0);
                location += muzzleYOffset;
                direction = (groundPosition - _playerTransform.position).normalized;
            }

            // todo: FindTargetAndLocation called many times
            //Debug.LogFormat("FindTargetAndLocation: location = {0}", location.ToString());
            if (!AStarPathfindingUtils.IsPointOnNavMeshOptimized(location))
            {
                return(false);
            }

            return(true);
        }
        else if (_playerTransform != null)
        {
            Plane playerPlane = new Plane(Vector3.up, _playerTransform.position);
            float enter       = 0;
            if (playerPlane.Raycast(ray, out enter))
            {
                Vector3 hitPoint = ray.GetPoint(enter);
                direction      = (hitPoint - _playerTransform.position).normalized;
                groundPosition = hitPoint;
            }
        }

        target   = null;
        location = default(Vector3);
        return(false);
    }