camera looks at waypoint upon collision

I have a player car (Gameobject with camera as child) and a series of waypoints.

I need the camera to look at the waypoint that the player car passes through. For example, as the player car passes through waypoints 1, 2, 3, 4, etc…, the camera will look at waypoints 1, 2, 3, 4, etc…

I also have no idea how to do this. I am very new to coding in both Javascript and C#.

Thank you,

kmb

hey Kmb,

Your post isn’t 100% clear to me… taking it literal you are asking for a cam that looks at a point the car drives through, so it has to look at the point the moment the car enters it?
while my guess is you want the camera to look at it before the car ran through it? correct me if I’m wrong…
anyway for both scenarios you’ll need to fill a list or array with the waypoint and itterate to the colesest/next waypoint and then you could for example use transform.Lookat();

it would look something like:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LookAtWayPoints : MonoBehaviour {

	private List<Vector3> wayPoints = new List<Vector3>();
	
	void Start () 
	{
		GameObject[] temp = GameObject.FindGameObjectsWithTag("waypoint");
		foreach(GameObject go in temp)
			wayPoints.Add(go.transform.position);
	}
	public Vector3 FindWayPoint ()
	{
		Vector3 positionOfClosest= new Vector3(0,0,0);
		
		if(wayPoints.Count > 0)
		{
			float dist= Mathf.Infinity;
			for(int i=0; i<wayPoints.Count; i++)
			{
				float closest= Vector3.Distance(wayPoints[i], transform.position);
				if(closest < dist)
				{	
					dist = closest;
					positionOfClosest = wayPoints[i];
					closest = 0;
				}
			}
			return positionOfClosest;
		}
		else
		{
			return transform.position;
		}
	}
	
	void Update () 
	{
		transform.LookAt(FindWayPoint());
	}
}

Sorry for the late response. AP Exams, SATs, and finals are pretty far from fun. That script was perfect!

Thank you,

kmb