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

Zooming in and out only when statement is true

Discussion in 'Scripting' started by tomtomh5, Aug 18, 2016.

  1. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    I've modeled and animated a pair of arms holding a weapon. When you press "Jump" the arms raise with the weapon into a "hipfire" position and you cannot return back to being unarmed. When you press "Fire2" the arms animate into a "aiming" position. I've included code that allows the FOV to change so it zooms in slightly when you press "Fire2". However you can press "Fire2" without the weapon drawn and it zooms in and out, how can I prevent that, so it only zooms in when you aim, and out when you release the "Fire2" key?
    I've tried various If statements but no success.
    My code so far :

    using UnityEngine;
    using System.Collections;

    public class TaserControllScript : MonoBehaviour {

    private Animator anim;

    // Use this for initialization
    void Start () {

    anim = GetComponent<Animator> ();
    }
    //Variables For A.D.S
    int zoom =30; //determines amount of zoom capable. Larger number means further zoomed in
    int normal =60; //determines the default view of the camera when not zoomed in
    float smooth =10; //determines speed of transition between zoomed in and default state
    private bool isZoomed = false; //boolean that determines whether we are in zoomed in state or not

    // Update is called once per frame
    void Update ()
    {

    if (Input.GetButton ("Jump"))
    {//Draw the taser
    anim.SetBool ("TaserDrawn", true);
    }
    else
    {
    //The taser will stay drawn!
    anim.SetBool ("TaserDrawn", false);
    }
    if (Input.GetButton ("Fire2"))
    {//Aims the Taser
    anim.SetBool ("TaserAim", true );

    //zooms in
    isZoomed = !isZoomed;
    GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, zoom, Time.deltaTime * smooth);

    }
    else
    {
    //returns to idle
    anim.SetBool ("TaserAim",false);
    //zooms out
    isZoomed = isZoomed;
    GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, normal, Time.deltaTime * smooth);
    }
    }
    }
     
  2. Nofinite

    Nofinite

    Joined:
    Aug 18, 2016
    Posts:
    1
    There are quite a few different ways I can think of to do what you are asking. To me it seems the easiest is to make a new function WeaponDrawn() that checks for a bool value confirming if it is drawn or not. Not sure if memory constraints allow it but probably a static boolean variable that can be accessed throughout game life.
     
  3. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    I've tried all sorts and its really bugging me. Could you give me some example code?
     
  4. lloydsummers

    lloydsummers

    Joined:
    May 17, 2013
    Posts:
    343
    You should wrap your code in the code paste option. I can't really see what your code is.

    Also, for things this specialized you should learn the concept and do the coding on your own... I'm not clear on what you want and the advice above looks good to me.

    When it isn't doing what you expect though, place a breakpoint and walk through the code to find the problem with the if statements.
     
  5. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    can you send a screenshot of your animation controller
     
  6. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    AnimController.png

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TaserControlScript : MonoBehaviour {
    5.  
    6.     private Animator anim;
    7.     //Variables For A.D.S
    8.     public int zoom =30; //determines amount of zoom capable. Larger number means further zoomed in
    9.     public     int normal =60; //determines the default view of the camera when not zoomed in
    10.     public    float smooth =10; //determines speed of transition between zoomed in and default state
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.      
    16.         anim = GetComponent<Animator> ();
    17.     }
    18.  
    19.  
    20.     // Update is called once per frame
    21.     void Update ()
    22.  
    23.     {
    24.      
    25.         if (Input.GetButton("Jump"))
    26.         {//Draw the taser
    27.          
    28.             anim.SetBool ("TaserDrawn", true);
    29.          
    30.  
    31.         }
    32.         else
    33.         {
    34.          
    35.             //The taser will stay drawn!
    36.             anim.SetBool ("TaserDrawn", false);
    37.  
    38.  
    39.         }
    40.        
    41.         if (Input.GetButton ("Fire2"))
    42.         {//Aims the Taser
    43.          
    44.             anim.SetBool ("TaserAim", true );
    45.  
    46.      
    47.             //zooms in
    48.      
    49.             GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, zoom, Time.deltaTime * smooth);
    50.  
    51.         }
    52.         else
    53.         {
    54.          
    55.             //returns to idle
    56.             anim.SetBool ("TaserAim",false);
    57.  
    58.             //zooms out
    59.         GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, normal, Time.deltaTime * smooth);
    60.         }
    61.  
    62.     }
    63. }
    64.  
    65.  
    66.  
     
  7. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    if i read good with my S***ty englisj you try to set the taser to draw to unarmed or from draw to idle
     
  8. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    "Jump" will play the animations in this order : unarmed > TaserDraw > TaserIdle
    Once TaserIdle is playing, you can't transition back to unarmed or TaserDraw
     
  9. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
  10. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    And how do i setup the transitions?
     
  11. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    Last edited: Aug 20, 2016
  12. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    i know how to create transitions.. what do i do with exit time, and parameters?
     
  13. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    just connect them just like i did on the picture
     
  14. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    yeah it doesn't work
     
  15. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    you could just add :
    animation.Play("name of animation") to the parts of your script that plays a certain animation like

    Code (CSharp):
    1. anim.SetBool("TaserAim", false);
    2.             animation.Play("TaserIdle");
    3.             isZoomed = false;
    4.             GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, normal, Time.deltaTime * smooth);
     
  16. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    Isn't there a way i can toggle the ability to aim on or off ?
     
  17. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    You can play with Bools like if i have a gun in my hand and i rightclick a bool isaiming = true, i cant do mutch with scripting now because im on an ipad
     
  18. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    I tried with bools and it didn't work, im not really sure what I did wrong.
     
  19. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    Do you have a pick-up gun script with a raycast in it?
     
  20. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    Well, im going furtur with this tomorrow because its 11pm here and im tired, if you send maybe more information Idk.
    If im on a pc tomorrow this Will be the first thing i work on. :)
     
  21. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    No its a rig with a pair of arms holding a gun, with an animation to draw the weapon, and then an idle animation, and an animation to aim (which is where i want to zoom in when aiming, and out when im not aiming)
     
  22. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    If(input.getmousebuttondown(1) && isAiming == false)
    {
    Play aim animation
    IsAiming = true;
    }
    If (input.getmousebuttondown(1) && isAiming == true)
    {
    Play idle animation
    IsAiming = false
    }

    Dont copy and paste because ive typed this on a ipad so i didnt have c# autocorrect thing
    But this sould give you an idea
     
  23. tomtomh5

    tomtomh5

    Joined:
    Oct 7, 2014
    Posts:
    39
    Finally figured it out, although i know its not the cleanest code but after the weapon is fired, if it hits the target it will be a "win" if it misses it will be "game over" so the code will function as I need it to. Thanks for your help.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TaserControlScript : MonoBehaviour {
    5.  
    6.     private Animator anim;
    7.     //Variables For A.D.S
    8.     public int zoom =30; //determines amount of zoom capable. Larger number means further zoomed in
    9.     public     int normal =60; //determines the default view of the camera when not zoomed in
    10.     public    float zoomIn =15; //determines speed of zooming in
    11.     public float zoomOut = 10; //determines the speed of zooming out
    12.     public bool weaponReady; //determines if the player has the taser equiped or is unarmed. If equiped player can aim(including zoom) and shoot
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         weaponReady = false;
    17.         anim = GetComponent<Animator> ();
    18.     }
    19.  
    20.  
    21.     // Update is called once per frame
    22.     void Update ()
    23.  
    24.     {
    25.         if (Input.GetButtonDown ("Jump")) {
    26.             //toggles weapon from unarmed to ready for zoom and firing
    27.             weaponReady = true;
    28.         }
    29.  
    30.         if (Input.GetButton("Jump"))
    31.         {
    32.             //Animates the taser being drawn
    33.             anim.SetBool ("TaserDrawn", true);
    34.            
    35.  
    36.         }
    37.         else
    38.         {
    39.            
    40.             //The taser will stay drawn!
    41.             anim.SetBool ("TaserDrawn", false);
    42.  
    43.  
    44.         }
    45.        
    46.         if (Input.GetButton ("Fire2"))
    47.         {
    48.             //animates the taser being aimed
    49.             anim.SetBool ("TaserAim", true );
    50.  
    51.             if (weaponReady) {
    52.                 //checks if the weapon is ready and allows it to zoom in when aimed
    53.        
    54.                 GetComponentInParent<Camera> ().fieldOfView = Mathf.Lerp (GetComponentInParent<Camera> ().fieldOfView, zoom, Time.deltaTime * zoomIn);
    55.             }
    56.         }
    57.         else
    58.         {
    59.            
    60.             //animates the taser into its idle animation
    61.             anim.SetBool ("TaserAim",false);
    62.  
    63.             //zooms out
    64.             GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, normal, Time.deltaTime * zoomOut);
    65.         }
    66.  
    67.     }
    68. }
    69.  
    70.  
    71.  
     
  24. dutchkiller2000

    dutchkiller2000

    Joined:
    May 13, 2016
    Posts:
    121
    i removed not inportant spaces, but the code looks clean for me(my code is always a heavy mess)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TaserControlScript : MonoBehaviour
    5. {
    6.  
    7.     private Animator anim;
    8.     //Variables For A.D.S
    9.     public int zoom = 30;
    10.     public int normal = 60;
    11.     public float zoomIn = 15;
    12.     public float zoomOut = 10;
    13.     public bool weaponReady;
    14.  
    15.     void Start()
    16.     {
    17.         weaponReady = false;
    18.         anim = GetComponent<Animator>();
    19.     }
    20.  
    21.     void Update()
    22.  
    23.     {
    24.         if (Input.GetButtonDown("Jump"))
    25.         {
    26.             weaponReady = true;
    27.         }
    28.         if (Input.GetButton("Jump"))
    29.         {
    30.             anim.SetBool("TaserDrawn", true);
    31.         }
    32.         else
    33.         {
    34.             anim.SetBool("TaserDrawn", false);
    35.         }
    36.  
    37.         if (Input.GetButton("Fire2"))
    38.         {
    39.             anim.SetBool("TaserAim", true);
    40.             if (weaponReady)
    41.             {
    42.                 GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, zoom, Time.deltaTime * zoomIn);
    43.             }
    44.         }
    45.         else
    46.         {
    47.             anim.SetBool("TaserAim", false);
    48.             GetComponentInParent<Camera>().fieldOfView = Mathf.Lerp(GetComponentInParent<Camera>().fieldOfView, normal, Time.deltaTime * zoomOut);
    49.         }
    50.     }
    51. }
    52.