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);
}
}
↧