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

How to move the Camera to a another position?

Discussion in 'Scripting' started by JediJaimie, Apr 4, 2015.

  1. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    How do I make a Trigger that makes the camera move to a other position? But I came across on this UnityAnswers but one of the answers seems like what I want but, I didn't understand the persons answer, Link: http://answers.unity3d.com/questions/57675/camera-on-trigger.html. This is a link to UnityAnswers, its about "cameras on trigger??" The Main Person Who posted the question was trying to do a some type of switch method with 2 cameras, but someone suggest him to move the camera instead. He did good step by step type of style but Its a javascript, I dont know much on javascript. Im trying to move the camera to a other position, do i have to do it with Mathf.Lerp? or with a bool? or something like this?

    Code (CSharp):
    1. void OnTriggerEnter2D (Collider2D other)
    2.     {
    3.         if (other.name == "Player")
    4.                      {
    5.                         mainCam.transform.position = transform.position;
    6.                      }
    7.  
    8.          }

    I'm trying to make a simple way to move the camera too a another position when the player OnTriggerEnter2D an empty gameObject. I only know C#.
     
  2. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    From what I know, the only difference bewteen C# and Javascript are the variables, the rest (I think...) are exactly the same (Citation needed).

    Anyway...

    This section of your code...
    Code (csharp):
    1.  mainCam.transform.position = transform.position;
    its telling the variable 'mainCam' to change its position to where the script is in the gameworld (the script is attached to a gameobject, so really its telling it to change its postion to where the gameobject is)

    If you want specifics...
    Code (CSharp):
    1.  mainCam.transform.position = transform.position = new Vector3(0, 0, 0);
    The above will change the objects position in the gameworld, just change the 0s to the desired coords

    or

    Code (CSharp):
    1.  transform.localPosition = new Vector3(0, 0, 0);
    The above will move your object X many to its X/Y/Z axis. Think as if your just moveing your mug 2cm to the left in real life, world coords are not taken into account when you do this.

    Am I on the right lines?
     
    JediJaimie likes this.
  3. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    Yes!? I'll have to play around with this more when I get the chance.

    Thank you!
     
  4. JediJaimie

    JediJaimie

    Joined:
    Feb 25, 2015
    Posts:
    10
    Anything else I need to know please post
     
  5. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    How'd it go?