Hi everybody,
İ want to make two game objects give their color to each other when they collide for a puzzle game. And also change moving direction but i succeeded it. I have the following code in my game objects named marbles besides components attached: rigidbody, sphere collider, mesh renderer and material. Marbles also have tags based on their colors. I.e. an indigo marble tag with Indigo. Marble colors are after rainbow colors so i have seven types of marbles, colors and corresponding tags. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Marble : MonoBehaviour
{
public float speed;
public Color color;
// Start is called before the first frame update
void Start()
{
color = gameObject.GetComponent<MeshRenderer>().material.color;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
transform.position += transform.forward * Time.deltaTime * speed;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Blue"))
{
speed = -speed;
color = new Color(0, 0, 255);
}
if (other.gameObject.CompareTag("Green"))
{
speed = -speed;
color = new Color(0, 255, 0);
}
if (other.gameObject.CompareTag("Indigo"))
{
speed = -speed;
color = new Color(75, 0, 130);
}
if (other.gameObject.CompareTag("Orange"))
{
speed = -speed;
color = new Color(255, 127, 0);
}
if (other.gameObject.CompareTag("Red"))
{
speed = -speed;
color = new Color(255, 0, 0);
}
if (other.gameObject.CompareTag("Violet"))
{
speed = -speed;
color = new Color(148, 0, 211);
}
if (other.gameObject.CompareTag("Yellow"))
{
speed = -speed;
color = new Color(255, 255, 0);
}
}
}