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. Dismiss Notice

Question about scripting cameras.

Discussion in 'Scripting' started by Snieh, Jan 20, 2021.

  1. Snieh

    Snieh

    Joined:
    Jan 12, 2021
    Posts:
    3
    To preface this question,I want everyone to know I am VERY new to C# and Unity in general. I started 2 days ago and I've never done any of this before. So, if you choose to answer this question, please explain it in a way a five year old could understand so I will be able to keep up. I apologize for the extra effort required on your part. Now, on to the question.

    I'm trying to learn how to make a 3D rail movement project and I have the Main Camera in the scene doing X and Y movements following the player avatar using this code.

    Code (CSharp):
    1.  
    2.     public float camSpeed = 0.125f;
    3.     public Vector3 offset;
    4.     public Transform target;
    5.        
    6.     void FixedUpdate()
    7.     {
    8.             //Gets position of target and current camera position and slowly aligns them according to camSpeed variable
    9.             Vector3 newPosition = target.position + offset;
    10.             transform.position = Vector3.Lerp(transform.position, newPosition, camSpeed);
    11.            
    12.             //Rotates Camera to face target
    13.             transform.LookAt(target);
    14.     }
    The next thing I want to do is clamp the movement of the target to be inside the field of view of the camera if it was in a stationary position and not following the target. So, I want to use a second camera to accomplish this task and never actually use it for rendering. My problem is that I don't know how to access this. I found this code online to accomplish the goal but it uses the main camera(which is already used to follow) and I don't know how to access other cameras in C#.

    Code (CSharp):
    1.         Vector3 pos = Camera.main.WorldToViewportPoint(transform.position);
    2.        
    3.         //Two math functions to ensure if X or Y is less than 0 it stays 0.  If X or Y greater than 1 then stays 1.
    4.         pos.x = Mathf.Clamp01(pos.x);
    5.         pos.y = Mathf.Clamp01(pos.y);
    6.         //Sets object position to new values.
    7.         transform.position = Camera.main.ViewportToWorldPoint(pos);
    How would I go about creating a second camera in the hierarchy named lockCamera and having this code apply to it?

    Thank you for your time and I apologize for the formatting errors and the strange question.
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Camera.main is a C# static, it's just a shortcut for the first camera in the scene tagged as main camera.

    you can use any instance of a camera instead of Camera.main
    Code (CSharp):
    1. Camera lockCamera;
    set it to public and reference it through the inspector or do it though code to the desired camera.


    clamping the movement to what you can see could be very hard to do in 3D, if it's with an orthographic camera should be pretty simple, I don't understand what you're thinking about doing with the second camera.


    also, if you're only two days in you might wanna spend some more time studying about basic programming and game architecture before trying to apply stuff on your own in a game engine.
     
  3. Snieh

    Snieh

    Joined:
    Jan 12, 2021
    Posts:
    3
    Thank you for the reply. I really appreciate it.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,755
    Camera stuff can be very tricky. There is a handy package called Cinemachine (look in the package manager) and it does a ton of basic camera stuff and is very easy to setup and use.

    That way you can focus on learning to program actual gameplay, which is where all the FUN is!