Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by blu4andor · Feb 25, 2012 at 10:26 AM · c#variablesinitializedeclareconstants

C# - Appropriate place for constants, variables, declarations, and initializations.

Hi guys. I'm new to Unity. Would anyone mind telling me the appropriate place for declaring and initializing constants and variables? For example, below is a script I wrote that animates a gun. At the top, I put a few constants that the user can adjust (things like where they want the weapon, where the weapon goes when they aim down the sights, a Camera that they can drag an object into in the inspector, then some protected variables that I use later in the class, and then a bunch of protected methods.

The code runs fine, but I don't think my approach is the way Unity wants me to do it. Anyone mind telling me if they see me doing anything wrong?

 using UnityEngine;
 using System.Collections;
 
 public class WeaponSway : MonoBehaviour {
     
     public Vector3 hipPos = new Vector3(.09f, -0.31f, .21f);
     public Vector3 adsPos = new Vector3(-0.0019f,-0.2482f,.09f);
     public Vector3 dropVector = new Vector3(0, -0.02f, 0);
     
     public float swayAngle = .2f;
     public float snapSpeed = 8f;
     
     public Camera mainCamera;
     
     public float hipFOV = 60;
     public float adsFOV = 40;
     
     Vector3 position;
     Vector3 positionST;
     Vector3 positionTG;
     float positionFR;
     
     float swayScale = 0;
     float swayScaleST = 0;
     float swayScaleTG = 0;
     float swayScaleFR = 0;
     
     float dropScale = 0;
     float dropScaleST = 0;
     float dropScaleTG = 0;
     float dropScaleFR = 0;
     
     float walkTimer = 0;
     
     float swayAngleX;
     float swayAngleY;
     Vector3 temp;
     
     float fovST;
     float fovTG;
     float fovFR;
     
     void Start() {
         
         position = hipPos;
         positionST = hipPos;
         positionTG = hipPos;
         positionFR = 1;
         
         fovST = hipFOV;
         fovTG = hipFOV;
         fovFR = 1;
     }
     
     void LateUpdate() {
         /* Updates the position of the gun based on stance and ADS state */
         position = Vector3.Lerp(positionST, positionTG, positionFR);
         dropScale = Mathf.Lerp(dropScale, dropScaleTG, dropScaleFR);
         swayScale = Mathf.Lerp(swayScale, swayScaleTG, swayScaleFR);
         
         swayAngleX  = Input.GetAxis("Mouse X") * swayAngle;
         swayAngleY  = Input.GetAxis("Mouse Y") * swayAngle * -1;
         
         /* Sets the position of the gun to the hip position, accounting for movement drop and sway. */
         transform.localPosition = position + (dropScale * dropVector) + (.005f * swayScale * swayVector());
         /* Sways the weapon according to mouse movement. */
         transform.Rotate(swayAngleY, swayAngleX, 0);
         /* Sets the camera FOV based on user actions and settings. */
         mainCamera.fieldOfView = Mathf.Lerp(fovST, fovTG, fovFR);
         
         /* Control Section:
         All code relating to getting input from the user and changing the appropriate variables. */
         
         /* If the player right-clicks, go to ADS */
         if(Input.GetButtonDown("Fire2") && positionTG == hipPos) {
             dropScaleTG = 0;
             dropScaleFR = 1;
             swayScaleTG = 0;
             swayScaleFR = 1;
             
             swayAngle = swayAngle / 2;
             
             fovST = mainCamera.fieldOfView;
             fovTG = adsFOV;
             fovFR = 0;
             
             positionST = transform.localPosition;
             positionTG = adsPos;
             positionFR = 0;
         }
         else if(Input.GetButtonDown("Fire2") && positionTG == adsPos) {
             swayAngle = swayAngle * 2;
             
             fovST = mainCamera.fieldOfView;
             fovTG = hipFOV;
             fovFR = 0;
             
             positionST = transform.localPosition;
             positionTG = hipPos;
             positionFR = 0;
         }
         
         /* If the player starts moving, drop the weapon */
         if((Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) && dropScaleTG == 0 && positionTG == hipPos) {
             dropScaleST = dropScale;
             dropScaleTG = 1;
             dropScaleFR = 0;
         }
         else if((Input.GetAxisRaw("Horizontal") == 0 && Input.GetAxisRaw("Vertical") == 0) && dropScaleTG == 1) {
             dropScaleST = dropScale;
             dropScaleTG = 0;
             dropScaleFR = 0;
         }
         
         /* If the player moves faster than 75% velocity, sway the weapon */
         if(dropScale > .75f && swayScaleTG == 0) {
             swayScaleST = swayScale;
             swayScaleTG = 1;
             swayScaleFR = 0;
         }
         else if(swayScale < .75f && swayScaleTG == 1) {
             swayScaleST = swayScale;
             swayScaleTG = 0;
             swayScaleFR = 0;
         }
         
         /* Updates all timers */
         positionFR += 8 * Time.deltaTime;
         fovFR += 8 * Time.deltaTime;
         swayScaleFR += 4 * Time.deltaTime;
         dropScaleFR += Time.deltaTime;
         walkTimer += 9 * Time.deltaTime;
         
         /* Snaps the gun back into position during rotations */
         temp = transform.localEulerAngles;
         temp.x = Mathf.LerpAngle(transform.localEulerAngles.x, 0, snapSpeed * Time.deltaTime);
         temp.y = Mathf.LerpAngle(transform.localEulerAngles.y, 0, snapSpeed * Time.deltaTime);
         temp.z = Mathf.LerpAngle(transform.localEulerAngles.z, 0, snapSpeed * Time.deltaTime);
         transform.localEulerAngles = temp;
     }
     
     /* Calculates which direction to sway the weapon */
     Vector3 swayVector() {
         return new Vector3(Mathf.Sin(walkTimer), Mathf.Abs(Mathf.Cos(walkTimer)), 0);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with var in C# (Vector3, transform and similiar) 1 Answer

Private Serialized vs Public, The Actual Effect on a Game 1 Answer

Local alternative to static variables? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges