'Set a time for the occurrence of a certain event at a certain point on the road in Unity
I need to know if is possible apply this change:
I have a driving simulator build using Unity. In a scene, there are some evets that leave when the player car passes through waypoints. Now this waypoints are in fixed position, the player car run some meters and when it passes through the waypont the event is triggered and started. Now I need to change this and I want that the player car can be run for some minutes before passes through the waypoint but this time should be managed from backend code.
In the follow image, you can display one of my waypoint

I had hypothesized if it was possible to lengthen the pieces of road but from the code, but I have no idea how and if it can be done. For the road I used this component EasyRoad
EDIT This is a RoadCrossEventTrigger, this class is called when the car pass into a waypoint.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DrivingTest
{
public class RoadCrossEventTrigger : MonoBehaviour
{
// Inspector variables
[SerializeField] private float characterSpeed;
[SerializeField] private float timeActive;
[SerializeField] private float factor;
[SerializeField] private GameObject roadCrossCharacter;
[SerializeField] private eCharacterType characterType;
// Private variables
private bool eventNotHappened = true;
public static eComplexity? testComplexity;
// Public variables
public static System.Action<eCharacterType> characterRoadCrossEvent;
/// <summary>
/// These triggers are placed a certain distance before the different events(kid/ dog/ bus man).
/// Whenever the player's car interacts with the trigger. The trigger starts a coroutine that executes the event.
/// </summary>
private void OnTriggerEnter(Collider other)
{
if(testComplexity != null && (testComplexity == eComplexity.Low || testComplexity == eComplexity.Medium))
{
//non devono partire eventi. Quindi no il bambino, no il cane, no la persona che scende dal bus.
}else if (testComplexity != null && testComplexity == eComplexity.High)
{
//devono partire eventi. Quindi il bambino, il cane, la persona che scende dal bus nel test TDF
eventNotHappened = false;
//Determines the time taken by the Player's car to reach the lane car after entering the trigger.
float timeToReach = Vector3.Magnitude(other.transform.position - roadCrossCharacter.transform.position) / other.gameObject.GetComponent<IRDSCarControllInput>().CarSpeed;
timeToReach *= factor;
StartCoroutine(SpawnCharacter(timeToReach));
}
else if (other.tag == Constants.PLAYER_TAG && eventNotHappened)
{
// Makes sure that the trigger fires the coroutine only the first time.
eventNotHappened = false;
//Determines the time taken by the Player's car to reach the lane car after entering the trigger.
float timeToReach = Vector3.Magnitude(other.transform.position - roadCrossCharacter.transform.position) / other.gameObject.GetComponent<IRDSCarControllInput>().CarSpeed;
timeToReach *= factor;
StartCoroutine(SpawnCharacter(timeToReach));
}
}
/// <summary>
/// Invokes the event that tells the CLNC class that the event has begun.
/// Moves the event character(kid/ dog/ bus man)
/// </summary>
/// <param name="time"> The time for which the coroutine waits before executing.</param>
IEnumerator SpawnCharacter(float time)
{
yield return new WaitForSeconds(time);
characterRoadCrossEvent?.Invoke(characterType);
if (roadCrossCharacter != null)
{
roadCrossCharacter.SetActive(true);
}
else
{
Debug.LogError("Road crossing character's reference is missing!!");
yield break;
}
float t = 0;
while (t < timeActive)
{
roadCrossCharacter.transform.Translate(Vector3.forward * characterSpeed * Time.deltaTime * 10);
t += Time.deltaTime;
yield return new WaitForEndOfFrame();
}
roadCrossCharacter.SetActive(false);
}
}
}
In SpwnCharaceter method if I insert this vlaue (60) the event start after 60 second, but after 60 seconds the user's car has already passed through the point where my event is located and therefore is not displayed.
Currently in point X there is the waypoint, on the road after a few meters my GameObject is positioned such as a child running in the middle of the road. Child waypoint distance is currently fixed. I would like to be able to manage it from code, I don't know if it is therefore possible to dynamically extend the road between the waypoint and the child.
Solution 1:[1]
New Solution
If I understand your issue correctly, you want to display something on the players current road x seconds after moving through a trigger.
Option A: set the event to become a child of the car, therefore it will follow the player wherever they go. (
transform.SetParent(car.transform); transform.localPosition = new Vector3.zero;)Option B: Add a "pressure plate" to every road to track which road the car is currently touching then set the event to activate on that road. (Perhaps even cut the roads in half so you don't activate it as a player leaves the road.)
Old Solution
Just create a timer that runs a function after x minutes once a player triggers a waypoint.
Option A: Use WaitForSeconds() Grepper Example Here
Option B: Use the update method with a float timer StackOverflow Example Here
Solution 2:[2]
Are you adding these 3 keys in your header correctly?
objectMetadata.setHeader("x-amz-server-side-encryption-customer-algorithm", ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key", key2)
objectMetadata.setHeader("x-amz-server-side-encryption-customer-key-MD5", md5)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Abdullah |

