简体   繁体   English

如何更改/设置 3d 立方体点击区域?

[英]How can I change/set the 3d cube clicking area?

Since the keypad don't have buttons I added cubes to each number and attached a script to each cube so when clicking on a cube it will show it's number in the top area.由于键盘没有按钮,我为每个数字添加了立方体并将脚本附加到每个立方体,因此当单击立方体时,它会在顶部区域显示其编号。

For example Key 1 (Cube) when I click on it when the game is running it's not firing the click on all the cube area same for all the other cubes :例如,当我在游戏运行时点击 Key 1 (Cube) 时,它不会触发对所有其他立方体的所有立方体区域相同的点击:

关键 1

When I click on the cube of key 1 it's working only on the left bottom area mostly on that sides.当我点击键 1 的立方体时,它只在左下角区域工作,主要是在那一边。 Even if the cube is covering the whole key area.即使立方体覆盖了整个关键区域。

This is how it looks like when I uncheck the cubes mesh renderer :这是我取消选中立方体网格渲染器时的样子:

未选中网格渲染器时的立方体

Then when running the game and clicking on a cube : it's showing the number in the Security Keypad Text(Text Mesh)然后在运行游戏并单击立方体时:它在安全键盘文本(文本网格)中显示数字

游戏运行时点击立方体

This is the script attached to each cube :这是附加到每个多维数据集的脚本:

using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

public class SecurityKeypadKeys : MonoBehaviour
{
    public TextMesh text;

    private void Start()
    {
        text = GameObject.Find("Security Keypad Text").GetComponent<TextMesh>();
    }

    private void OnMouseDown()
    {
        string[] digits = Regex.Split(transform.name, @"\D+");
        foreach (string value in digits)
        {
            int number;
            if (int.TryParse(value, out number))
            {
                text.text = value;
            }
        }
    }
}

The main problem is that on most of the cubes you need to click on specific point/small area to make it work and I want it to be working trigger when you click on each cube at any place on the cube.主要问题是,在大多数立方体上,您需要单击特定点/小区域才能使其工作,我希望它在您单击立方体上任何位置的每个立方体时触发工作。

Try the RaycastExample by robertbu code here and put it on a script.这里尝试 robertbu 代码的 RaycastExample 并将其放在脚本上。 OnMouseDown() requires nothing else be in the way. OnMouseDown() 不需要其他任何东西。 That code will tell you if something is in the way.该代码会告诉您是否有问题。 Also, try moving everything closer as OnMouseDown() has a depth limit to its raycast.此外,尝试将所有内容移近,因为 OnMouseDown() 对其光线​​投射有深度限制。

I recreated your above example, and it works perfectly, so something else must be wrong.我重新创建了你上面的例子,它工作得很好,所以一定是其他地方有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM