Hello!
I am using Unity version 6000.0.26f1 and I have an issue regarding Visual Scripting. I usually only write C#-scripts because to me Visual Scripting can be a bit confusing and frustrating, but for an assignment I need to have two Script Graphs.
I want to implement the logic that when the Event “UFO_Landed” is triggered (by a C#-Script that makes the UFO land in front of the player), a UI screen should appear that asks the player if s:he wants to explore the UFO.
So I have this Boolean value linked to “UFO_Landed” and if it is checked the UI screen will appear immediately after entering Play mode, if not it will never appear. I can’t seem to find what’s wrong.
I’d really appreciate if somebody could help me.
Thanks in advance for looking over it!
Find attached the screenshot of my script graph and below my C#-script for the UFO-landing.
I wish all of you a nice day.
C#-script:
using UnityEngine;
using Unity.VisualScripting;
public class UFO_Landing : MonoBehaviour
{
public Transform player; // Reference to the player object
public Camera playerCamera; // Reference to the player's camera
public float landingDistance = 5f; // Distance from the camera to land the UFO
public float landingSpeed = 5f; // Speed at which the UFO lands
public float landingHeightOffset = 1f; // Vertical offset for landing relative to the camera's view
public AudioSource landingAudio; // Audio source for landing sound
public ParticleSystem landingParticles; // Particle system to trigger on landing
public GameObject UIManager; // Reference to the GameObject with the UI script attached
private Vector3 targetPosition; // Calculated target position for landing
private bool isLanding = false; // Is the UFO currently landing?
private bool audioPlayed = false; // Flag to ensure audio plays only once
private bool landingComplete = false; // Flag to check if landing is complete
void Start()
{
Debug.Log("UFO_Landing script initialized.");
if (player == null || playerCamera == null)
{
Debug.LogError("Player or PlayerCamera reference is missing! Please assign them in the Inspector.");
return;
}
if (UIManager == null)
{
Debug.LogError("UIManager reference is missing! Please assign it in the Inspector.");
return;
}
// Automatically start the landing process when entering Play mode
LandInFrontOfCamera();
}
void Update()
{
if (isLanding && !landingComplete)
{
// Move the UFO toward the target position
transform.position = Vector3.MoveTowards(transform.position, targetPosition, landingSpeed * Time.deltaTime);
// Add a slow rotation effect while landing
transform.Rotate(Vector3.up, 50f * Time.deltaTime);
// Check distance to target position
float distanceToTarget = Vector3.Distance(transform.position, targetPosition);
Debug.Log($"UFO distance to target: {distanceToTarget}");
// Play landing audio only once as the UFO approaches the target
if (landingAudio != null && !audioPlayed && distanceToTarget < 10f)
{
PlayLandingAudio();
audioPlayed = true; // Prevent multiple audio playback
}
// Check if the UFO has reached the target position
if (distanceToTarget < 0.1f)
{
Debug.Log("UFO has landed successfully.");
TriggerLandingEffects();
CompleteLanding();
}
}
}
// Method to calculate landing position in front of the camera and start the landing process
public void LandInFrontOfCamera()
{
if (playerCamera != null)
{
// Calculate a position in front of the camera's forward direction
targetPosition = playerCamera.transform.position + playerCamera.transform.forward * landingDistance;
// Adjust the height offset
targetPosition.y += landingHeightOffset;
Debug.Log($"Landing initiated. Target position: {targetPosition}");
isLanding = true;
audioPlayed = false; // Reset audio playback flag for a new landing
}
else
{
Debug.LogError("PlayerCamera reference is not assigned! Unable to land.");
}
}
// Ensure the audio source is playing
private void PlayLandingAudio()
{
if (landingAudio != null && !landingAudio.isPlaying)
{
landingAudio.Play();
Debug.Log("Landing audio is now playing.");
}
else if (landingAudio != null && landingAudio.isPlaying)
{
Debug.Log("Landing audio is already playing.");
}
else
{
Debug.LogWarning("Landing audio source is missing!");
}
}
// Trigger effects when the UFO lands
private void TriggerLandingEffects()
{
// Play the particle system instantly
if (landingParticles != null)
{
landingParticles.Play();
Debug.Log("Landing particles triggered.");
}
else
{
Debug.LogWarning("Landing particle system is missing.");
}
}
// Notify Visual Scripting when landing is complete
private void CompleteLanding()
{
isLanding = false; // Stop landing logic
landingComplete = true; // Mark landing as complete
Debug.Log("Triggering Custom Event: UFO_Landed in Visual Scripting.");
// Trigger a custom event for Visual Scripting
if (UIManager != null)
{
CustomEvent.Trigger(UIManager, "UFO_Landed");
Debug.Log("Custom Event UFO_Landed triggered.");
}
else
{
Debug.LogError("UIManager is null. Cannot trigger custom event.");
}
}
}