简体   繁体   English

Raycast统一击中游戏对象

[英]Raycast hit game object in unity

I created a 3d main menu in unity, each button is a cube. 我统一创建了一个3d主菜单,每个按钮都是一个立方体。 I enabled the user to press on the button by raycast hit, the problem is that I want to change the cube color when the raycast is on the cube and convert it back to the original color when the raycast exit the cube. 我使用户能够通过raycast hit按下按钮,问题是我想在raycast在多维数据集上时更改多维数据集颜色,并在raycast退出多维数据集时将其转换回原始颜色。 I'm using c# and I read about "OnMouseEnter" and "OnMouseExit" - I'm not using a mouse but using game controller (Razer Hydra). 我正在使用c#,并且阅读了有关“ OnMouseEnter”和“ OnMouseExit”的信息-我不是在使用鼠标,而是在使用游戏控制器(Razer Hydra)。 How can I simulate OnMouseEnter and OnMouseExit in raycast? 如何在raycast中模拟OnMouseEnter和OnMouseExit?

From what i understand you want to raycast on the cube and change its color and change it back when the cursor or ray cast is not hitting it, I would suggest using this logic without mouseEnter and mouseExit. 据我了解,您想在多维数据集上进行光线投射并更改其颜色,并在光标或光线投射未命中它时将其更改回,我建议在没有mouseEnter和mouseExit的情况下使用此逻辑。

RaycastHit hit; RaycastHit热门歌曲;

void Update () {

   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if (Physics.Raycast(ray, out hit)) {
      if (hit.collider.tag == "cube"){
        //Change color here
        }

    }else {

    // Change back to prvious color.

} } }}

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

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