Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D raycasting mouse to select items

Discussion in '2D' started by Robster95, Mar 5, 2020.

  1. Robster95

    Robster95

    Joined:
    Jul 3, 2019
    Posts:
    152
    I am having trouble implementing a raycast into some code i've already had that works.

    What i'm trying to do is use a raycast from a mouse to select one of 2 dogs on the screen and when selected the camera will zoom into the dog.

    before implementing the raycast i would just have it be a button press and the camera would zoom into the dog with little to no problems. now the console is saying there's no instance of an object when i also see that the objects are selected with the raycast.

    here is the code below: please if anyone can help with figuring out why the console is saying theres no reference i would appreciate any help!

    Thank you,

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

    public class Camera_move : MonoBehaviour
    {
    public float cameraChangeSpeed = 2;
    private Transform custompos;

    Vector3 maincamerapos, transitionCamera;
    Transform tr;
    bool maincampos;

    public float smooth = 4;
    public float min = 2;
    public float max = 15;
    public float targetO;

    void Start()
    {
    tr = transform;
    maincamerapos = tr.position;
    }



    void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    //Converting Mouse Pos to 2D (vector2) World Pos
    Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    RaycastHit2D hit = Physics2D.Raycast(rayPos, Vector2.zero, 0f);

    if (hit)
    {
    custompos = hit.transform;
    Debug.Log(hit.transform.name);

    Debug.Log(custompos);
    }

    if (maincampos == false)
    {
    maincampos = true;
    tr.parent = custompos;
    }
    else
    {
    maincampos = false;
    tr.parent = null;
    }
    Debug.Log(tr.parent);
    }

    // transition to dog
    if (maincampos == true)
    {
    //transitionCamera = custompos.position;
    transitionCamera = new Vector3(custompos.position.x + 3, custompos.position.y, -10f);

    targetO = Mathf.Clamp(targetO, min, max);
    Camera.main.orthographicSize = Mathf.MoveTowards(Camera.main.orthographicSize, targetO, smooth * Time.deltaTime);
    }

    else transitionCamera = maincamerapos;

    // transition to main camera position
    if (tr.position != transitionCamera)
    {
    tr.position = Vector3.MoveTowards(tr.position, transitionCamera, Time.deltaTime * cameraChangeSpeed);

    targetO = Mathf.Clamp(targetO, min, max);
    Camera.main.orthographicSize = Mathf.MoveTowards(Camera.main.orthographicSize, 5, smooth * Time.deltaTime);
    }
    }
    }
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Here you create a raycast with no direction (Vector3.zero), so even if you have your mouse on an object, the ray will not "intersect" with the collider. I recommend using a Physics2D.OverlapPoint() to get an array of colliders under your mouse instead. You can replace the code above with something like this:
    Code (CSharp):
    1. Vector2 mousePos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
    2.  
    3. Collider2D[] collidersUnderMouse = new Collider2D[4];
    4. int numCollidersUnderMouse = Physics2D.OverlapPoint(mousePos, new ContactFilter2D(), collidersUnderMouse);
    5.  
    6. for (int i = 0; i < numCollidersUnderMouse; ++i)
    7. {
    8.     // Check if collidersUnderMouse[i] is the type of object you want using tags or GetComponent()
    9.     // Then do what you want to it
    10. }
    Keep in mind if there are multiple colliders under your mouse, you will only detect a certain number of them up to the size of the Collider2D array (I put 4, you can put more if you think you need it).
     
    MelvMay likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    The abvice above is good so you should follow it. Don't use 3D physics techniques like using raycast for selection as in 2D that makes sense, especially using a degenerate raycast of zero length.

    Finally, I would ask that you use code-tags for this post and ones for the future, thanks.

    https://forum.unity.com/threads/using-code-tags-properly.143875/