Certainly! Below is the implementation of the OnDrawGizmos method which will draw a thick line from the current transform to the objectRenderer's position:
void OnDrawGizmos()
{
if (objectRenderer != null)
{
// Set the color for the Gizmos
Gizmos.color = Color.red;
// Store the current Matrix
Matrix4x4 oldMatrix = Gizmos.matrix;
// Adjust the gizmo matrix to draw a thick line
Gizmos.matrix = Matrix4x4.TRS(Vector3.zero, transform.rotation, Vector3.one);
// Draw the line from the current position to the object renderer's position
Vector3 startPosition = transform.position;
Vector3 endPosition = objectRenderer.transform.position;
// Calculate the direction and distance
Vector3 direction = (endPosition - startPosition).normalized;
float distance = Vector3.Distance(startPosition, endPosition);
// Draw the thick line by making multiple lines around the original line
float thickness = 0.1f; // Adjust this for a thicker or thinner line
int segments = 10; // Adjust the number of segments for smoother or rougher line
for (int i = 0; i < segments; i++)
{
float t = (float)i / (segments - 1);
Vector3 offset = new Vector3(Mathf.Sin(t * Mathf.PI * 2) * thickness, Mathf.Cos(t * Mathf.PI * 2) * thickness, 0);
Gizmos.DrawLine(startPosition + offset, endPosition + offset);
}
// Restore the old Gizmos matrix
Gizmos.matrix = oldMatrix;
}
}
This OnDrawGizmos method will draw a red, thick line between the Transform of the current GameObject and the Transform of objectRenderer.
Please make sure to place this method in your TycoonScript class. This will allow you to visualize the connection in the scene view when the Gizmos are enabled.
Here is your complete script with OnDrawGizmos() method incorporated:
using UnityEngine;
using TMPro;
using UnityEngine.Events;
public class TycoonScript : MonoBehaviour
{
[Header("Object Trigger Settings")]
[Space]
public LayerMask objectLayer; // Specific layer mask for triggering
[Header("Audio Settings")]
[Space]
public AudioClip specificAudioClip; // Specific AudioClip for playOneshot
public float volumeChangeAmount = 0.5f; // Amount to change volume by
[Header("Object Info")]
[Space]
public float price; // Price of the object
public bool bought; // Whether the object is bought or not
public TextMeshPro text; // TextMeshPro component for displaying price
[Header("Events")]
[Space]
public UnityEvent onTriggerEnterEvent; // Event invoked on trigger enter
public Renderer objectRenderer; // Renderer component reference onTriggerEnterEvent; // Event invoked on trigger enter
public Material highlight; // Renderer component reference onTriggerEnterEvent; // Event invoked on trigger enter
public Mesh objectColllider; // Renderer component reference onTriggerEnterEvent; // Event invoked on trigger enter
[SerializeField] private float speed = 20f;
void Update()
{
transform.Rotate(new Vector3(0, speed * Time.deltaTime, 0));
}
public void OnEnable()
{
objectRenderer = GetComponent<Renderer>(); // Get Renderer component on start
if (objectRenderer != null)
{
objectRenderer.enabled = false;
}
if (text != null)
{
text.text = price.ToString(); // Display price when script is enabled
}
if (!bought)
{
objectRenderer.material = highlight;
}
}
public void OnDisable()
{
objectRenderer = GetComponent<Renderer>(); // Get Renderer component on start
if (objectRenderer != null)
{
objectRenderer.enabled = false;
}
}
public void OnTriggerEnter(Collider other)
{
if (((1 << other.gameObject.layer) & objectLayer) != 0)
{
onTriggerEnterEvent?.Invoke(); // Invoke the UnityEvent
Play3DAudio(specificAudioClip); // Play 3D spatialized audio when triggered
UnlockTycoon();
}
}
public void Play3DAudio(AudioClip clip)
{
if (clip != null)
{
// Create a new GameObject for the AudioSource
GameObject audioObj = new GameObject("TempAudioSource");
AudioSource tempAudioSource = audioObj.AddComponent<AudioSource>();
tempAudioSource.clip = clip;
tempAudioSource.spatialBlend = 1.0f; // Set audio to be 3D spatialized
tempAudioSource.volume = 1.0f; // Default volume, can be adjusted
tempAudioSource.PlayOneShot(clip); // Play the audio clip once
// Optionally, destroy the audio object after the clip has played
Destroy(audioObj, clip.length / tempAudioSource.pitch);
}
}
public void UnlockTycoon()
{
objectRenderer.enabled = true;
}
void OnDrawGizmos()
{
if (objectRenderer != null)
{
// Set the color for the Gizmos
Gizmos.color = Color.red;
// Store the current Matrix
Matrix4x4 oldMatrix = Gizmos.matrix;
// Adjust the gizmo matrix to draw a thick line
Gizmos.matrix = Matrix4x4.TRS(Vector3.zero, transform.rotation, Vector3.one);
// Draw the line from the current position to the object renderer's position
Vector3 startPosition = transform.position;
Vector3 endPosition = objectRenderer.transform.position;
// Calculate the direction and distance
Vector3 direction = (endPosition - startPosition).normalized;
float distance = Vector3.Distance(startPosition, endPosition);
// Draw the thick line by making multiple lines around the original line
float thickness = 0.1f; // Adjust this for a thicker or thinner line
int segments = 10; // Adjust the number of segments for smoother or rougher line
for (int i = 0; i < segments; i++)
{
float t = (float)i / (segments - 1);
Vector3 offset = new Vector3(Mathf.Sin(t * Mathf.PI * 2) * thickness, Mathf.Cos(t * Mathf.PI * 2) * thickness, 0);
Gizmos.DrawLine(startPosition + offset, endPosition + offset);
}
// Restore the old Gizmos matrix
Gizmos.matrix = oldMatrix;
}
}
}