Hi there,
I have two separate gameObjects and I want to interact with both of them at the same time when the mouse is hovering over just one of the objects. Is there a way to connect the objects or their colliders?
thanks in advance
Hi there,
I have two separate gameObjects and I want to interact with both of them at the same time when the mouse is hovering over just one of the objects. Is there a way to connect the objects or their colliders?
thanks in advance
two possibilities come to mind… make 1 the child of the other. or add have one simply control the other.
That doesn’t work sadly.
what exactly do you mean by that? can you give an example?
You should give an example of what happens when you “control” the first object… some code where the object is hovering over one of the objects. then I could give you a practical example of how to do the same with the other object.
So this is the code for the highlight effect I want when hovering over the object and not pressing the left mouse button. Now I have two separate objects on which I want this effect at the same time when I only hover over either of them.
public class ChangeEmission : MonoBehaviour
{
private Material _material;
private Color _targetColor = new Color(0.2f, 0.25f, 0.5f, 0.5f);
public bool displayInfo;
bool mousePressed;
bool mouseOver;
void Start()
{
_material = GetComponent<Renderer>().material;
}
void Update()
{
click ();
EmissionChange ();
if (mouseOver && !mousePressed)
{
displayInfo = true;
}
else
{
displayInfo = false;
}
}
void click()
{
if (Input.GetMouseButtonDown(0)){
mousePressed = true;
} else if (Input.GetMouseButtonUp(0)){
mousePressed = false;
}
}
void OnMouseOver ()
{
mouseOver = true;
}
void OnMouseExit ()
{
mouseOver = false;
}
void EmissionChange ()
{
if (displayInfo)
{
_material.SetColor("_EmissionColor", _targetColor);
}
else
{
_material.SetColor("_EmissionColor", Color.black);
}
}
}
try something like this:
public class ChangeEmission : MonoBehaviour
{
[HideInInspector]
public Material _material;
private Color _targetColor = new Color(0.2f, 0.25f, 0.5f, 0.5f);
public bool displayInfo;
bool mousePressed;
bool mouseOver;
public ChangeEmission linkedObject;
void Start()
{
_material = GetComponent<Renderer>().material;
}
void Update()
{
click ();
EmissionChange ();
if (mouseOver && !mousePressed)
{
displayInfo = true;
}
else
{
displayInfo = false;
}
}
void click()
{
if (Input.GetMouseButtonDown(0)){
mousePressed= true;
} else if (Input.GetMouseButtonUp(0)){
mousePressed = false;
}
}
void OnMouseOver ()
{
mouseOver= true;
}
void OnMouseExit ()
{
mouseOver = false;
}
void EmissionChange ()
{
if (displayInfo)
{
_material.SetColor("_EmissionColor", _targetColor);
if(linkedObject != null && linkedObject._material != null){
linkedObject._material.SetColor("_EmissionColor", _targetColor);
}
}
else
{
_material.SetColor("_EmissionColor", Color.black);
if(linkedObject != null && linkedObject._material != null){
linkedObject._material.SetColor("_EmissionColor", Color.black);
}
}
}
}