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

Question How to make a scope cam that's optional

Discussion in 'Scripting' started by TenshouYoku, Apr 23, 2023.

  1. TenshouYoku

    TenshouYoku

    Joined:
    Apr 22, 2023
    Posts:
    2
    Greetings everyone,
    I am completely new to Unity (or just coding in general) and am trying to make a mecha game.

    One part of this mecha game is that I'm trying to build a system where it's possible to switch between 3 points of view:

    1. Standard 3rd person mode,
    2. Standard 1st person mode, and
    3. Aim Down Sight mode, only when the weapon is equipped with a scope.

    The problem regarding to this is that, with some googlefu I can figure out a general idea of how to switch between modes (very likely not correct as depicted below);

    Code (CSharp):
    1. public GameObject ThirdCam;
    2. public GameObject FirstCam;
    3. public GameObject ScopeCam;
    4. public int CamMode;
    5.  
    6. void Update() {
    7. if(Input.GetButtonDown("Camera"))
    8. if(CamMode==2){CamMode = 0;}else{CamMode+-=1;}
    9. if(CamMode>2 and CamMode<0){CamMode=0)}
    10. }
    11.  
    12. IEnumerator CamChange(){
    13. yield return new WaitForSeconds(0.01f);
    14. if (CamMode==0){
    15. ThirdCam.SetActive(true);
    16. FirstCam.SetActive(false);
    17. ScopeCam.SetActive(false);
    18. if (CamMode==1)
    19. ThirdCam.SetActive(false);
    20. FirstCam.SetActive(true);
    21. ScopeCam.SetActive(false);
    22. if (CamMode==2)
    23. ThirdCam.SetActive(false);
    24. FirstCam.SetActive(false);
    25. ScopeCam.SetActive(true);
    26. }
    But if I want to ensure ScopeCam is possible only if the player has a weapon that is equipped with ScopeCam, how do I detect whenever ScopeCam is equipped and allow ScopeCam view to be true, and vice versa?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    Otherwise if you insist on rolling all this camera code yourself, start with some good tutorials on it. It's insanely complex and could never be adequately described in a little text box here.
     
  3. StarBornMoonBeam

    StarBornMoonBeam

    Joined:
    Mar 26, 2023
    Posts:
    209
    If the weapon is not equipped and if CamMode > 2 then CamMode can = 0.
     
  4. TenshouYoku

    TenshouYoku

    Joined:
    Apr 22, 2023
    Posts:
    2
    In code-wise, how do I check if Weapon is equipped with ScopeCam?
    I probably should learn cinemachine instead i think, thanks