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 Tutorial Help

Discussion in 'Scripting' started by ott0bronz, Jun 11, 2023.

  1. ott0bronz

    ott0bronz

    Joined:
    Mar 20, 2023
    Posts:
    2
    the tutorial instructions are:
    Allow the player to press a key on the keyboard to switch camera views.


    Ideally, the same key would toggle between two views, one above and behind the vehicle, and the other from the perspective of the driver’s seat.

    this is what I coded:
    Code (CSharp):
    1. {
    2.     public GameObject player;
    3.     private Vector3 inVehicle = new Vector3(0,2,1);
    4.     private Vector3 offset = new Vector3(0,5,-7);
    5.  
    6.     void LateUpdate()
    7.     {
    8.  
    9.         if(Input.GetKeyDown(KeyCode.Keypad0))
    10.  
    11.         {
    12.             transform.position = player.transform.position + inVehicle;
    13.         }
    14.         else
    15.         {
    16.             transform.position = player.transform.position + offset;
    17.         }
    18.      
    19.     }
    20. }
    21.  
    i have found a solution but I don't understand why it works by adding these lines
    Code (CSharp):
    1. {
    2.     public GameObject player;
    3.     private Vector3 offset = new Vector3(0, 6, -9);
    4.     private Vector3 offsetFPV = new Vector3(0, 3, 1);
    5.     private bool viewFPV = false;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void LateUpdate()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.Alpha1)) {
    17.             if (viewFPV) { viewFPV = false; } else { viewFPV = true; }
    18.         }
    19.  
    20.         if (viewFPV) {
    21.             transform.position = player.transform.position + offsetFPV;
    22.         } else {
    23.             transform.position = player.transform.position + offset;
    24.         }
    25.     }
    26. }
     
  2. ott0bronz

    ott0bronz

    Joined:
    Mar 20, 2023
    Posts:
    2
    I understand now smh, is there a more optimal way to toggle view?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,558
    Optimal in what way?! Are you sure you are focusing on the right thing here???

    Imphenzia: How Did I Learn To Make Games:



    OTHERWISE:

    DO NOT OPTIMIZE "JUST BECAUSE..." If you don't have a problem, DO NOT OPTIMIZE!

    If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

    Window -> Analysis -> Profiler

    Failure to use the profiler first means you're just guessing, making a mess of your code for no good reason.

    Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

    https://forum.unity.com/threads/is-...ng-square-roots-in-2021.1111063/#post-7148770

    Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

    Notes on optimizing UnityEngine.UI setups:

    https://forum.unity.com/threads/how...form-data-into-an-array.1134520/#post-7289413

    At a minimum you want to clearly understand what performance issues you are having:

    - running too slowly?
    - loading too slowly?
    - using too much runtime memory?
    - final bundle too large?
    - too much network traffic?
    - something else?

    If you are unable to engage the profiler, then your next solution is gross guessing changes, such as "reimport all textures as 32x32 tiny textures" or "replace some complex 3D objects with cubes/capsules" to try and figure out what is bogging you down.

    Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

    This sort of speculative optimization assumes you're properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.