- Home /
changing the camera's local position with Vector3 variables coming from an array
hello everybody,
i am student in Unity and working on a school project. I have a car with 3 different camera angles: backCam, insideCam and hoodCam... as I named them.
I would like to iterate through an array storing the positions of these cams as Vector3 vector positions.
I could store the positions in Vector3 (float) variables but i am not really familiar with the usage of foreach. We just touched the topic but still unsure how to use local variables in foreach.
This is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraChange : MonoBehaviour
{
Vector3 backCamPos = new Vector3((float)-0.0270000007, (float)1.36899996, (float)-2.70099998);
Vector3 insideCamPos = new Vector3((float)-0.282001942, (float)0.851599991, (float)-0.113700002);
Vector3 hoodCam = new Vector3((float)- 0.0270000007, (float)0.953000009, (float)0.453000009);
public Vector3[] cameraVectorArray;
int index = 0;
// Start is called before the first frame update
void Start()
{
cameraVectorArray = new Vector3[] { backCamPos, insideCamPos, hoodCam };
transform.localPosition = backCamPos;
}
// Update is called once per frame
void Update()
{
ChangeCamera();
}
void ChangeCamera()
{
if(Input.GetKeyDown(KeyCode.C))
{
int index = 0;
//transform.localPosition = insideCamPos;
foreach(Vector3 vector in cameraVectorArray)
{
transform.localPosition = vector.x;
index++;
}
}
}
}
I do know that the code is not working, I am missing something somewhere or using the foreach iteration on a bad way.
Can I get some help please?
Thank you
Answer by ArtyD2 · Nov 29, 2021 at 02:55 PM
void ChangeCamera()
{
if(Input.GetKeyDown(KeyCode.C))
{
int index = 0;
//transform.localPosition = insideCamPos;
foreach(Vector3 vector in cameraVectorArray)
{
transform.localPosition = vector.x;
index++;
}
}
}
This is a good start however, you need to be setting the local position to the whole vector, not just the x value. Also, this will always result in only switching you to the last value in the array (it actually sets the local position to each value in the array but because this is all happening in the same frame, you will only see it going to the last value), there actually isn't a need to use a foreach in this implementation.
first we should change it so that it only iterates through to the next value in the array once on KeyDown this can be done by storing the index externally to the function and making it a member of the class, we can call it cameraIndex.
int cameraIndex = 0
Then we want to change it so that each time we press the key, we increase the index by one and then change our position to that position:
void ChangeCamera()
{
if(Input.GetKeyDown(KeyCode.C))
{
cameraIndex++;
transform.locaclPosition = cameraVectorArray[cameraIndex];
}
}
However, this will always increase the value of the index and will give us errors if we go over the size of the array so lets add some safety:
void ChangeCamera()
{
if(Input.GetKeyDown(KeyCode.C))
{
cameraIndex++;
if (cameraIndex > cameraVectorArray.Size;)
{
cameraIndex = 0;
}
transform.localPosition = cameraVectorArray[cameraIndex];
}
}
I think the full script would end up looking something like this
public class CameraChange : MonoBehaviour
{
Vector3 backCamPos = new Vector3((float)-0.0270000007, (float)1.36899996, (float)-2.70099998);
Vector3 insideCamPos = new Vector3((float)-0.282001942, (float)0.851599991, (float)-0.113700002);
Vector3 hoodCam = new Vector3((float)- 0.0270000007, (float)0.953000009, (float)0.453000009);
public Vector3[] cameraVectorArray;
int cameraIndex = 0;
// Start is called before the first frame update
void Start()
{
cameraVectorArray = new Vector3[] { backCamPos, insideCamPos, hoodCam };
transform.localPosition = backCamPos;
}
// Update is called once per frame
void Update()
{
ChangeCamera();
}
void ChangeCamera()
{
if(Input.GetKeyDown(KeyCode.C))
{
cameraIndex++;
if (cameraIndex > cameraVectorArray.Size;)
{
cameraIndex = 0;
}
transform.localPosition = cameraVectorArray[cameraIndex];
}
}
I hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613050004im_/https://answers.unity.com/themes/thub/images/avi.jpg)