- Home /
Moving a GameObject (camera) forward and backward along a series of points (C#).
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!");
}
Answer by mpavlinsky · Feb 21, 2012 at 10:39 PM
You don't need to refer to the class name since you're already in a method of the class. You just have to call the GetClosestEnemy function and get the position out of the transform it returns. I guess first you have to get an array of enemy transforms to pick one from. You can do this either by having a public field in your Get class that is a Transform array that you could assign transforms to in the editor or you could get them programmatically from the scene using GameObject.FindGameObjectsWithTag or something like that and then either change the GetClosestEnemy function to accomodate an array of GameObjects instead of Transforms or convert the array to one of Transforms. After you have the array of transforms you call the function like:
Camera.main.transform.position = GetClosestEnemy(ENEMIES_ARRAY_GOES_HERE).position;
Thanks so much for replying, but I really don't know enough about coding to execute your instructions (sorry!). I did however find something which I think is similar to your suggestion which can be found here: http://answers.unity3d.com/questions/10986/transform-array-in-javascript.html (I know it says javascript, but the answerer posted c# as well). If I create a transform array similar to the one in the link, then how would I call the transform in my script? Would it be anything like "GetClosestEnemy(waypoints).position" or something like? Thanks again.
Yes the suggestion in your link would work for this. Of course you would have to name the nodes "p#" in your scene where # is the number of the node starting from 1. Then you would call GetClosestEnemy() exactly as you assumed.
I don't particularly like the method suggested in that link because it is sloppy and you need your objects named sequentially among other potential issues, but it should be fine to get you started.
BTW: You may want to look at the free iTween plugin (http://itween.pixelplacement.com/index.php), which was designed to do exactly what you've described. It builds a curve from a series of points and allows you to move backwards and forwards over that curve.
Stupendous! It works; thanks chaps. I posted the final code above so people from the future could have a peruse if they want.
I do have one last query though, but it's purely out of interest; what does Places in
"Transform GetPoint(Transform[] Places)"
refer to? I though it was the tag, but the script works regardless of tags, so a bit of a mystery.
Where are you seeing this function?
In that context Places is just the name it gives the variable sent in so it has an alias when it uses it inside the function and it doesn't need to know the name the variable was called before entering the function. If you're asking what the Places array is supposed to represent it's hard to say because the function name and parameter name aren't particularly telling.