Search Unity

[SOLVED] Raycast and OnMouseEnter trigger off center / off middle

Discussion in 'Physics' started by Inferi, Oct 19, 2018.

  1. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    I have tried using Raycast and OnMouseEnter and OnMouseOver to check if player is over a button made from a 3dCube in Unity.

    It works with all these methods but it triggers off center "off middle" from top and bottom on the buttons. See clip:



    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ElevatorChooseFloorButton : MonoBehaviour {
    6.     private ElevatorHandler _ElevatorHandler;
    7.  
    8.     public Animator ElevatorInnerDoorsAnimator;
    9.     public Camera MainCamera;
    10.     public GameObject ButtonDestination;
    11.  
    12.     public Canvas c;  // Temporary
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         _ElevatorHandler = GameObject.Find("ApartmentBuilding01_Elevator").GetComponent<ElevatorHandler>();
    17.  
    18.         c = GameObject.Find("Canvas_Interaction").GetComponent<Canvas>(); // Temporary
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update () {
    23.         // This code gives same results as OnMouseEnter or OnMouseOver
    24.  
    25.         //if (ElevatorInnerDoorsAnimator.GetBool("resting") == true && _ElevatorHandler.HasDestination == false) {
    26.         //    Ray ray;
    27.         //    RaycastHit hit;
    28.         //    ray = MainCamera.ScreenPointToRay(Input.mousePosition);
    29.         //    if (Physics.Raycast(ray, out hit, 4)) {
    30.         //        if (hit.collider.tag == "FloorChooserButton") {
    31.         //            if (hit.transform.name == this.transform.name) {
    32.         //                if (Input.GetMouseButtonUp(0)) {
    33.         //                    ElevatorInnerDoorsAnimator.SetBool("resting", false);
    34.         //                    ElevatorInnerDoorsAnimator.SetBool("open", false);
    35.         //                    ElevatorInnerDoorsAnimator.SetBool("close", true);
    36.         //                    _ElevatorHandler.HasDestination = true;
    37.         //                    _ElevatorHandler.ElevatorGotoFloor = ButtonDestination;
    38.         //                    _ElevatorHandler.ElevatorButton = hit.transform.gameObject;
    39.         //                }
    40.         //            }
    41.         //        }
    42.         //    }
    43.  
    44.         //}
    45.  
    46.  
    47.     }
    48.  
    49.  
    50.     private void OnMouseEnter() {
    51.         if (ElevatorInnerDoorsAnimator.GetBool("resting") == true && _ElevatorHandler.HasDestination == false) {
    52.             c.GetComponent<CanvasGroup>().alpha = 1f; // Temporary
    53.             if (Input.GetMouseButtonUp(0)) {
    54.                 ElevatorInnerDoorsAnimator.SetBool("resting", false);
    55.                 ElevatorInnerDoorsAnimator.SetBool("open", false);
    56.                 ElevatorInnerDoorsAnimator.SetBool("close", true);
    57.                 _ElevatorHandler.HasDestination = true;
    58.                 _ElevatorHandler.ElevatorGotoFloor = ButtonDestination;
    59.                 _ElevatorHandler.ElevatorButton = this.transform.gameObject;
    60.             }
    61.         }
    62.     }
    63.  
    64.  
    65.     private void OnMouseExit() {
    66.         c.GetComponent<CanvasGroup>().alpha = 0f; // Temporary
    67.     }
    68.  
    69.  
    70. }
    71.  
    What am I doing wrong here!? Been pulling my hair on this thing today :)
     
  2. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    Any ideas?
     
  3. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    It seems that if I move the colliders Y centers to 0.11 the raycast reacts as it should.

    I really do not think this is the correct way to solve this problem and I am still in need of some ideas on what makes this happen.

    I have checked if the white dot is in the middle of my playing area and it is so that should not be the case?.


    UPDATE:
    When I debug the ray, the ray comes out from the middle of the cube as it should. Still it will trigger 0.11f down from the cube top

    UPDATE:
    The further away I get, the bigger the offset!? Something is really wrong here.
     
    Last edited: Oct 20, 2018
  4. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    SOLVED

    I need to use

    Code (CSharp):
    1. ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    I guess that is correct as I dont really use the mousecursor, I only use center of screen as the cursor is hidden by the First Person Controller :D