下面是原代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouse1控制 : MonoBehaviour
{
public GameObject a;
public GameObject b;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit pengdaodewuti;
bool shubiaoshifoudiandao = Physics.Raycast(shubiaoweizhi, out pengdaodewuti);
if (pengdaodewuti . collider . gameObject . name == "a" && shubiaoshifoudiandao == true)
{
a.GetComponent<gaibia_a_deyanse>().enabled = true;//物体a的颜色改变
}
else
{
a.GetComponent<gaibia_a_deyanse>().enabled = false;//物体a的颜色变为原样
}
if (pengdaodewuti . collider . gameObject . name == "b" && shubiaoshifoudiandao == true)
{
b.GetComponent<gaibia_b_deyanse>().enabled = true;//物体a的颜色改变
}
else
{
b.GetComponent<gaibia_b_deyanse>().enabled = false;//物体a的颜色变为原样
}
}
else
{
a.GetComponent<gaibia_a_deyanse>().enabled = false;//物体a的颜色变为原样
b.GetComponent<gaibia_b_deyanse>().enabled = false;//物体a的颜色变为原样
}
}
}
我想要的效果:鼠标对着物体a按住鼠标左键,物体a颜色改变;移动鼠标使鼠标位置离开物体a,物体a颜色变回原样;再将鼠标移动到物体b上,物体b颜色改变;移动鼠标使鼠标位置离开物体b,物体b的颜色变回原样。(整个过程中都是按住鼠标左键不放)
实际效果:鼠标对着物体a按住鼠标左键,物体a颜色改变;移动鼠标使鼠标位置离开物体a,物体a颜色没变;再将鼠标移到物体B上,物体b颜色改变以及物体a颜色变回原样;移动鼠标时鼠标位置离开物体b,物体b的颜色不变,除非松开鼠标左键。
8友们,我这个代码应该怎么改才能达到我想要的效果?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mouse1控制 : MonoBehaviour
{
public GameObject a;
public GameObject b;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit pengdaodewuti;
bool shubiaoshifoudiandao = Physics.Raycast(shubiaoweizhi, out pengdaodewuti);
if (pengdaodewuti . collider . gameObject . name == "a" && shubiaoshifoudiandao == true)
{
a.GetComponent<gaibia_a_deyanse>().enabled = true;//物体a的颜色改变
}
else
{
a.GetComponent<gaibia_a_deyanse>().enabled = false;//物体a的颜色变为原样
}
if (pengdaodewuti . collider . gameObject . name == "b" && shubiaoshifoudiandao == true)
{
b.GetComponent<gaibia_b_deyanse>().enabled = true;//物体a的颜色改变
}
else
{
b.GetComponent<gaibia_b_deyanse>().enabled = false;//物体a的颜色变为原样
}
}
else
{
a.GetComponent<gaibia_a_deyanse>().enabled = false;//物体a的颜色变为原样
b.GetComponent<gaibia_b_deyanse>().enabled = false;//物体a的颜色变为原样
}
}
}
我想要的效果:鼠标对着物体a按住鼠标左键,物体a颜色改变;移动鼠标使鼠标位置离开物体a,物体a颜色变回原样;再将鼠标移动到物体b上,物体b颜色改变;移动鼠标使鼠标位置离开物体b,物体b的颜色变回原样。(整个过程中都是按住鼠标左键不放)
实际效果:鼠标对着物体a按住鼠标左键,物体a颜色改变;移动鼠标使鼠标位置离开物体a,物体a颜色没变;再将鼠标移到物体B上,物体b颜色改变以及物体a颜色变回原样;移动鼠标时鼠标位置离开物体b,物体b的颜色不变,除非松开鼠标左键。
8友们,我这个代码应该怎么改才能达到我想要的效果?