Answer the question
In order to leave comments, you need to log in
How to click on buttons in 3D?
There are 4 buttons in the field of view of the fixed camera. Buttons are ordinary cubes with colliders. The mouse is not fixed and is used to control the game. When you click on each of the buttons, a different event should occur.
At the moment the code is like this (below). It works only if the camera is rotated, and the mouse is in the center, and there is a beam "shot" current in front of the camera. How to remake it so that it works with a fixed camera and a moving mouse?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonClick : MonoBehaviour {
[SerializeField] private Transform aimPoint;
[SerializeField] private float range;
[SerializeField] private string leftRedTag;
[SerializeField] private string leftWhiteTag;
[SerializeField] private string rightRedTag;
[SerializeField] private string rightWhiteTag;
private void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
if (Physics.Raycast(aimPoint.position, aimPoint.forward, out hit, range))
OnButtonClick(hit.transform.gameObject.tag);
}
}
private void OnButtonClick(string tag) {
if (tag == leftRedTag) {
// код левой красной кнопки
} else if (tag == leftWhiteTag) {
// код левой белой кнопки
} else if (tag == rightRedTag) {
// код правой красной кнопки
} else if (tag == rightWhiteTag) {
// код правой белой кнопки
}
}
}
Answer the question
In order to leave comments, you need to log in
You raycast somewhere forward from some transform, and you need to raycast towards the cursor. Camera.ScreenPointToRay returns a ray that goes from the camera to a location on the screen. Example:
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity))
{
Debug.DrawRay(hit.point, hit.normal * 10, Color.red, 10f);
}
public interface IButton
{
public void OnClick();
}
public class YellowButton : IButton
{
public void OnClick()
{
Debug.Log("Yellow");
}
}
public class Example : MonoBehaviour
{
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity))
{
if(hit.transform.TryGetComponent(out IButton button))
{
button.OnClick();
}
}
}
}
}
public class Button : MonoBehaviour
{
public event Action ButtonPressed;
private void OnMouseDown()
{
ButtonPressed?.Invoke();
}
}
public class ButtonHandler : MonoBehaviour
{
[SerializeField] private Button _button;
private void OnEnable()
{
_button.ButtonPressed += DoStuff;
}
private void OnDisable()
{
_button.ButtonPressed -= DoStuff;
}
private void DoStuff()
{
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question