I am making a chemistry molecule game. So far I have a molecule with a trigger on a receptor site and when I drag the correct atom close to this site, the atom changes colour and attaches. How do I change the colour of the whole molecule (not just the trigger/receptor site) once this atom is attached?
Code used for this part of my game is from: https://gamedev.stackexchange.com/questions/123929/snap-an-object-to-another-object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snap : MonoBehaviour
{
public string partnerTag;
public float closeVPDist = 0.05f;
public float farVPDist = 1;
public float moveSpeed = 40.0f;
public float rotateSpeed = 90.0f;
private Vector3 screenPoint;
private Vector3 offset;
private bool isSnaped;
Color color = new Color(2, 1, 3);
float dist = Mathf.Infinity;
Color normalColor;
GameObject partnerGO;
// Use this for initialization
void Start()
{
normalColor = GetComponent<Renderer>().material.color;
partnerGO = GameObject.FindGameObjectWithTag(partnerTag);
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Cursor.visible = false;
}
void OnMouseDrag()
{
//transform.SetParent(null);
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
Vector3 partnerPos = Camera.main.WorldToViewportPoint(partnerGO.transform.position);
Vector3 myPos = Camera.main.WorldToViewportPoint(transform.position);
dist = Vector2.Distance(partnerPos, myPos);
GetComponent<Renderer>().material.color = (dist < closeVPDist) ? color : normalColor;
}
void OnMouseUp()
{
Cursor.visible = true;
if (dist < closeVPDist)
{
transform.SetParent(partnerGO.transform);
StartCoroutine(InstallPart());
isSnaped = true;
}
if (dist > farVPDist)
{
// transform.SetParent(null);
}
}
IEnumerator InstallPart()
{
while (transform.localPosition != Vector3.right || transform.localRotation != Quaternion.identity)
{
transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.right, Time.deltaTime * moveSpeed);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, Time.deltaTime * rotateSpeed);
yield return new WaitForEndOfFrame();
}
}
}
In OnMouseUp
, add a line to change the material/colour of your molecule. Code:
public Color newColor;
public GameObject objectToChangeColour;
void OnMouseUp()
{
Cursor.visible = true;
if (dist < closeVPDist)
{
transform.SetParent(partnerGO.transform);
StartCoroutine(InstallPart());
isSnaped = true;
objectToChangeColour.GetComponent<Renderer>().material.color = newColour; // Change the color of object to the newColour
}
if (dist > farVPDist)
{
// transform.SetParent(null);
}
}
And simple change objectToChangeColour
to the object you want to change its color (parentGO/molecule/etc.), and newColour
to the new colour.