Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

void Start()

Discussion in 'Scripting' started by Cubies, Apr 3, 2019.

  1. Cubies

    Cubies

    Joined:
    May 1, 2015
    Posts:
    11
    Is it possible using the void Start() function to position the camera location and rotation so I could just drag 'n drop the C# script onto the camera asset so when I click run it positions the camera at the correct location?
     
    Last edited: Apr 3, 2019
  2. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    Do you mean while the game is running?
     
  3. Cubies

    Cubies

    Joined:
    May 1, 2015
    Posts:
    11
    Yes, I would like the camera to shift from the default location to the new updated location in the void Start() function while the game is running.
     
  4. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    Maybe use a if statement in the update funktion in your camera script. And add a button with void onGui. If you push the button a bool variable = true and if its true the position and rotation will change.
     
  5. Cubies

    Cubies

    Joined:
    May 1, 2015
    Posts:
    11
    I'd like to know how to change the camera location and rotation in the void Start() function
     
  6. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    Change the transform.position and the transform.rotation
     
  7. Cubies

    Cubies

    Joined:
    May 1, 2015
    Posts:
    11
    That's what I'm trying to figure out how to do.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    If you already know the position and rotation you want it to have, you literally just say

    Code (CSharp):
    1. transform.position = myDesiredPosition;
    2. transform.rotation = myDesiredRotation;
    If you're trying to figure out how to calculate the appropriate position and rotation, then it depends what you are trying to accomplish

    If you know that the desired camera position isn't going to change in the future, then your simplest option is probably to position a camera manually in edit mode and use the camera preview to align it exactly where you want it, then just copy the position and rotation values from the inspector into your code.

    If you're trying to position it automatically according to some rules (like "focus on the player"), then you will have to formalize those rules and translate them into code so that the computer can follow them.
     
  9. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    position is a Vector3, you gotta give it a x, y, z coordinate.
    so transform.position = new Vector3(x, y, z) or someGameObject.transform.position

    rotation is a Quaternion, but if you use Quaternion.Euler you can give it an x, y, z rotation as well.
    so transform.position = new Quaternion.Euler(x, y, z) or someGameObject.transform.rotation.

    all you need is a location you want to place the camera on. you can make this either a gameobject in the scene or a vector3 variable. same thing for the orientation.
     
  10. K_O_N_S_T_Y

    K_O_N_S_T_Y

    Joined:
    Feb 24, 2019
    Posts:
    50
    Search on youtube „change transform form game objekts in unity“ and you will find what you need