- Home /
Confine object to path/waypoints while tracking player?
The question itself is difficult to word properly but pretty much I have an audio source for a stream that I want to track the player while also being confined to a path that runs along the stream itself. How can I do this?
Answer by Unified2000 · May 31, 2018 at 07:38 PM
You would have to create an array of points that run along the stream and then search for the point that is closest to the player. If your stream is a mesh object then you could avoid the need for an array of points and instead search for the vertex within the stream's mesh that is closest to the player.
Or just manually drop lots of audio sources along your stream and avoid the mess above :)
Edit:
Here's a script that tracks the player along a path:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Streamy : MonoBehaviour {
public Transform streamPoints; // This should reference the parent transform of the stream's waypoints/gameobjects. Three points minimum
int currentPoint=0;
int nearestPoint=0;
float nearestDistance=Mathf.Infinity;
Vector3 nearestPosition;
Vector3 p1;
Vector3 p2;
Vector3 p3;
void Update() {
Vector3 playerPosition=Camera.main.transform.position;
if (currentPoint<streamPoints.childCount-1)
{
if ((streamPoints.GetChild(currentPoint).position-playerPosition).sqrMagnitude<nearestDistance) {
nearestPoint=currentPoint;
nearestDistance=(streamPoints.GetChild(currentPoint).position-playerPosition).sqrMagnitude;
}
currentPoint++;
}
else
{
p1=streamPoints.GetChild(nearestPoint).position;
p2=streamPoints.GetChild(nearestPoint+1).position;
if (Vector3.Dot((p2-p1).normalized,(playerPosition-p1).normalized)<0) {
p2=streamPoints.GetChild(nearestPoint-1).position;
if (Vector3.Dot((p2-p1).normalized,(playerPosition-p1).normalized)<0) p2=p1;
}
nearestPosition=p1+Vector3.Project(Vector3.ClampMagnitude(playerPosition-p1,(p2-p1).magnitude),(p2-p1).normalized);
currentPoint=1;
nearestDistance=Mathf.Infinity;
}
transform.position=Vector3.Lerp(transform.position,nearestPosition,2*Time.deltaTime);
}
}
Tried the simplified method of dropping a ton of audio sources. Didn't work out as there was a lot of overlap with the ranges.
I've just had a go and I managed to get the audio sources working quite well. I set Spatial Blend to max and then set the $$anonymous$$ax Distance to 60 meters. I then edited the volume rolloff curves to be max for 0 to 40 meters and then curve down to zero for the remaining 20 meters. The audio sources have a 20 meter overlap.
The most important part is making the first 40 meters flat because this prevents the listener from identifying the source of the sound because the volume stops increasing when moving closer to the source.
Your answer
Follow this Question
Related Questions
Waypoint Setup: How? 4 Answers
Determine transform.position of location between two waypoints on bezier path 0 Answers
Modifying regular waypoint moving code to curved path 0 Answers
What is the best AI system for pathfinding on a moving platform 2 Answers
How do you generate a line between objects that are only instantiated upon game Start ? 1 Answer