[Solved]Circle selection menu with raycast problem/bug.

Hello community this is my first thread, i try to fix all by myself searching similar problems in the forum, but i can’t find this, so here is the problem.

I am working on a selection “Menu” for the moods/skills of the character.

This look like this. (The script is “Selector Animo” (Mood Selector))

It seem to work good, but here is the bug.

The ray works fine sometimes and randomly fails, i can’t identify why this happen.

So here is the code (Just the “Selection” part).

void Seleccion(){

        if (Input.GetMouseButton(0)) {
            RaycastHit2D hit = Physics2D.Raycast(RayStart.position, RayEnd.position, 10f, LayerMask.GetMask("Selector"));

//Dibuja linea en scene.
            Debug.DrawLine(RayStart.position, RayEnd.position, Color.red);
            //Diferentes casos, activa el booleano de la direccion a la cual el raycast esta tocando.
            if (hit.collider != null && hit.collider.tag == "Neutro") {
                Neutro = true;
            }
            else {
                Neutro = false;
            }

            if (hit.collider != null && hit.collider.tag == "Triste"){
                Triste = true;
            }
            else {
                Triste = false; 
            }

            if (hit.collider != null && hit.collider.tag == "Sensible"){
                Sensible = true;
            }
            else {
                Sensible = false;
            }

            if (hit.collider != null && hit.collider.tag == "Asustado"){
                Asustado = true;
            }
            else {
                Asustado = false;
            }

            if (hit.collider != null && hit.collider.tag == "Agresivo"){
                Agresivo = true;
            }
            else {
                Agresivo = false;
            }

            }

        else {
            followmouse.OnSelection = false;
            //Desactiva selector
            Selector.SetActive(false);
        }
    }

I hope you can understand my problem, my english get this harder i know, sorry for that.

Thanks for your time!

I guess you problem is withing the calculation of the RayEnd vector so your ray doesn’t hit any gameobject. Could you please show the regarding code?

Thanks, i already check that, now the vector RayEnd is fixed, but still bugged. The strange thing here is that some times the ray hit the collider, and sometimes not. Anyways here is the code.

using UnityEngine;
using System.Collections;

public class MouseFollowGeneric : MonoBehaviour {

    Vector3 MousePosition;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        MousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        transform.position = new Vector3 (MousePosition.x, MousePosition.y, transform.position.z);
    }
}

See, here is other bug… I am loosing my mind with this.

Okay i already solved this, for anyone having this problem here is the solution.

I was putting the a transform.position in the second parameter. Like this:

RaycastHit2D hit = Physics2D.Raycast(RayStart.position, rayend , 10f, LayerMask.GetMask("Selector"));

Solution:

RaycastHit2D hit = Physics2D.Raycast(RayStart.position, rayend - RayStart.position, 10f, LayerMask.GetMask("Selector"));