Search Unity

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

Discussion in 'Scripting' started by germangguerci, Oct 4, 2015.

  1. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    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).

    Code (CSharp):
    1. void Seleccion(){
    2.  
    3.         if (Input.GetMouseButton(0)) {
    4.             RaycastHit2D hit = Physics2D.Raycast(RayStart.position, RayEnd.position, 10f, LayerMask.GetMask("Selector"));
    5.  
    6. //Dibuja linea en scene.
    7.             Debug.DrawLine(RayStart.position, RayEnd.position, Color.red);
    8.             //Diferentes casos, activa el booleano de la direccion a la cual el raycast esta tocando.
    9.             if (hit.collider != null && hit.collider.tag == "Neutro") {
    10.                 Neutro = true;
    11.             }
    12.             else {
    13.                 Neutro = false;
    14.             }
    15.  
    16.             if (hit.collider != null && hit.collider.tag == "Triste"){
    17.                 Triste = true;
    18.             }
    19.             else {
    20.                 Triste = false;
    21.             }
    22.  
    23.             if (hit.collider != null && hit.collider.tag == "Sensible"){
    24.                 Sensible = true;
    25.             }
    26.             else {
    27.                 Sensible = false;
    28.             }
    29.  
    30.             if (hit.collider != null && hit.collider.tag == "Asustado"){
    31.                 Asustado = true;
    32.             }
    33.             else {
    34.                 Asustado = false;
    35.             }
    36.  
    37.             if (hit.collider != null && hit.collider.tag == "Agresivo"){
    38.                 Agresivo = true;
    39.             }
    40.             else {
    41.                 Agresivo = false;
    42.             }
    43.  
    44.             }
    45.  
    46.         else {
    47.             followmouse.OnSelection = false;
    48.             //Desactiva selector
    49.             Selector.SetActive(false);
    50.         }
    51.     }
    52.  
    53.  
    I hope you can understand my problem, my english get this harder i know, sorry for that.

    Thanks for your time!
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    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?
     
  3. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    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.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MouseFollowGeneric : MonoBehaviour {
    5.  
    6.     Vector3 MousePosition;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         MousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    16.         transform.position = new Vector3 (MousePosition.x, MousePosition.y, transform.position.z);
    17.     }
    18. }
    19.  
     
  4. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    See, here is other bug... I am loosing my mind with this.

     
  5. germangguerci

    germangguerci

    Joined:
    Oct 4, 2015
    Posts:
    6
    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:

    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(RayStart.position, rayend , 10f, LayerMask.GetMask("Selector"));
    2.  
    Solution:


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