Unity 3d Interaction System Glitching

So in this unity project I have created a interaction to glorify that which counter I am currently facing so depending on that I can spawn related items from it but the game object should only Console “Selected” if I am looking towards it only but it’s happening Even if I am not looking towards it or is far away from it after first enabling it, it even sometimes becomes disabled when I face the counter again which is totally against the script I just wrote So any kind of help will be much appreciated.

Interactions Function

private void HandleInteractions()
{
    Vector2 inputVector = gameInput.GameMovementVectorNormalized();

    //This Transform relates to the actual transfor of the Project Asset aur in this case Player
    Vector3 movDir = new Vector3(inputVector.x, 0f, inputVector.y);

    if (movDir != Vector3.zero)
    {
        lastInteractDir = movDir;
    }

    float interactDistance = 1f;

    //This method will detect collision and will return the collision objects reference too for interaction purposes
    //Here we used the lastInteractDir to even if if stopped moving towards and interactable object
    //we still get that objects reference to use it
    if (Physics.Raycast(transform.position, lastInteractDir, out RaycastHit raycastHit, interactDistance, countersLayerMask))
    {
        if (raycastHit.transform.TryGetComponent(out ClearCounter clearCounter))
        {
            //Has Clear Counter
            //clearCounter.Interact();
            if (clearCounter != selectedCounter)
            {
                SetSelectedCounter(clearCounter);
                //Here
            }
            else
            {
                //something in front but doesn't has clear counter script
                SetSelectedCounter(null);
                //Here
            }
        }
        else
        {
            //if nothing infront
            SetSelectedCounter(null);
            //Here
        }

    }
    //can use RaycastAll() to cycle through many possible objects to get interactions
    //or can use the Raycast(15) to use the layers mask method and handle interactions
    //based on Layers
}

Invoke Function

private void SetSelectedCounter(ClearCounter selectedCounter)
{
    //this funtion so we dont have to duplicate code again and agian in the usage areas
    this.selectedCounter = selectedCounter;

    onSelectedCounterChanged?.Invoke(this, new onSelectedCounterChangedEventArgs
    {
        //making cross interaction between for selected counter and Clear counter per say
        selectedCounter = selectedCounter
    });
}

Event
    public event EventHandler<onSelectedCounterChangedEventArgs> onSelectedCounterChanged;
    public class onSelectedCounterChangedEventArgs : EventArgs
    {
        //This is the proper convention for Event Creation
        public ClearCounter selectedCounter;
    }

Interaction Function

private void GameInput_OnInteractAction(object sender, System.EventArgs e)
    {
        if (selectedCounter != null)
        {
            //if counterinfront is not null then Interation can be made
            selectedCounter.Interact();
        }
    }


Activity Handler

private void Start()
{
    Player.Instance.onSelectedCounterChanged += Player_onSelectedCounterChanged;
}
private void Player_onSelectedCounterChanged(object sender, Player.onSelectedCounterChangedEventArgs e)
{
    if (e.selectedCounter == clearCounter)
    {
        Show();
    }
    else
    {
        Hide();
    }
}

private void Show()
{
    visualGameObject.SetActive(true);
}

private void Hide()
{
    visualGameObject.SetActive(false);
}

Interact Function Logging

public void Interact()
    {
        Debug.Log("Interact!");
    }


Thanks a bunch!

New contributor

USMAN GHAUSE is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật