Hello Answerers!
N.B. I am a noob at scripting & coding, my sincerest apologies.
I have a series of locations that I have specified with gameobjects, I have a simple system whereby I move the camera to a specific location thus:
using UnityEngine;
using System.Collections;
public class MoveCameraTo1 : MonoBehaviour {
void OnMouseDown() {
Camera.main.transform.position = GameObject.Find("pos1").transform.position;
Debug.Log("we're going to Pos1!");
}
However, I would also like to move the camera forward or backward along this series (formed of pos1,2,3 etc.). Should I be using an array or list, or way-points or something of that nature? I have also seen scripts that try to find the closest object of a type, but seeing as these points will be equally spaced, I think that would be more troublesome, wouldn’t it?
Thanks in advance to anyone who can help!
EDIT!
I have been trying to use this, setting the positions to enemies:
using UnityEngine;
using System.Collections;
public class Get : MonoBehaviour {
Transform GetClosestEnemy(Transform[] enemies)
{
Transform tMin = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = transform.position;
foreach (Transform t in enemies)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
void OnMouseDown() {
Camera.main.transform.position = Get.transform.position;
Debug.Log("we're going forward!");
}}
But I have likely borked it up; I dont know what tMin is or how to refer to Get in my OnMouseDown function (is it even a function? I just don’t know anymore).
EDIT2!
Here is the functional code I spliced:
Transform[] pointlist = new Transform[3];
void Start()
{
for (int n=0; n < pointlist.Length; ++n)
{
pointlist[n] = GameObject.Find("pos"+(n+1)).transform;
}
}
Transform GetPoint(Transform[] Places)
{
Transform tMin = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = transform.position;
foreach (Transform t in pointlist)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
void OnMouseDown() {
Camera.main.transform.position = GetPoint(pointlist).position;
Debug.Log("We're going forward!");
}