Does Raycast2D not work?

0

I need to do a raycasthit2d on an image but it only works only when I move the camera manually over the image, but I need to do it with the mouse position I have:

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

 public class raycast : MonoBehaviour {
 public LayerMask layer;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
RaycastHit2D hit =Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition),Vector2.zero);

if(hit.collider != null){
    Debug.Log("yaaaa");
}

}
}
    
asked by jose moya 03.09.2018 в 17:32
source

1 answer

-1

using UnityEngine.AI;

public class MoveToClickPoint : MonoBehaviour {
    NavMeshAgent agent;

    void Start() {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update() {
        if (Input.GetMouseButtonDown(0)) {
            RaycastHit hit;

            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) {
                agent.destination = hit.point;
            }
        }
    }
}

/// link

    
answered by 17.09.2018 в 10:53