Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tamas3000 · Nov 29, 2021 at 02:17 PM · arrayvector3iterate

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tamas3000 · Nov 29, 2021 at 04:45 PM

Thank you very much for the help.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing Vector3s of GameObjects in Arrays (JS) 1 Answer

Trying to make a for loop with an array of Vector3 0 Answers

2D Array and other Script method access 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Vector3 Array Empties Its Self Immediately After Being Initialized 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges