Quantcast
Channel: Questions in topic: "gun"
Viewing all articles
Browse latest Browse all 592

Aim Down Sights and Weapon Sway not working unless animations are not assigned

$
0
0
Hi, Aim down sights and weapon sway won't work unless in the animator the actual animations aren't assigned. (Except Idle, Idle works fine.) ![alt text][1] This is currently my setup under the animator tab, script will explain why I do not have a transition from Idle to Reload. ![alt text][2] [1]: /storage/temp/96051-capture.png [2]: /storage/temp/96052-capture.png And finally, here are my scripts: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Weapon : MonoBehaviour { private Animator anim; //Animator component public Image Crosshair; public AudioSource _AudioSource; //Audio Source public float range = 100f; // Range of weapons public int bulletsPerMag = 30; // Bullets per Mag public int bulletsLeft = 200; // Total bullets public int currentBullets; // Current bullets in current mag public enum ShootMode {Auto, Semi}; public ShootMode shootingMode; public Transform shootPoint; //point where bullet spawns public GameObject hitParticles; public GameObject bulletImpact; public ParticleSystem muzzleFlash; // Magic Flashy stuff public AudioClip shootSound; //shoot sound public float fireRate = 0.1f; //rate of fire public float damage = 20f; float fireTimer; private bool isReloading; private bool shootInput; // Use this for initialization void Start () { currentBullets = bulletsPerMag; // set current bullets to bullets per Mag anim = GetComponent (); //Get Animator component. //_AudioSource = GetComponent (); } // Update is called once per frame void Update () { switch(shootingMode) { case ShootMode.Auto: shootInput = Input.GetButton("Fire1"); break; case ShootMode.Semi: shootInput = Input.GetButtonDown("Fire1"); break; } if (shootInput) { if (currentBullets > 0) Fire (); //Play fire function else if(bulletsLeft > 0) DoReload (); } if (Input.GetKeyDown (KeyCode.R)) { if (currentBullets < bulletsPerMag && bulletsLeft > 0) { DoReload (); } } if (fireTimer < fireRate) fireTimer += Time.deltaTime; } void FixedUpdate() { AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo (0); isReloading = info.IsName ("reload"); /*if (info.IsName ("Shoot")) { anim.SetBool ("Fire", false); }*/ } private void Fire() { if (fireTimer < fireRate || currentBullets <= 0 || isReloading) return; RaycastHit hit; if (Physics.Raycast (shootPoint.position, shootPoint.transform.forward, out hit, range)) { Debug.Log (hit.transform.name + " found"); GameObject hitParticleEffect = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)); //hitParticleEffect.transform.SetParent(hit.transform); GameObject bulletHole = Instantiate(bulletImpact, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); bulletHole.transform.SetParent(hit.transform); Destroy(hitParticleEffect, 1f); Destroy(bulletHole, 20f); if(hit.transform.GetComponent()) { hit.transform.GetComponent().ApplyDamage(damage); } } anim.CrossFadeInFixedTime ("Shoot", 0.01f); muzzleFlash.Play (); // show muzzle flash //anim.SetBool ("Fire", true); currentBullets--; // deduct bullet fireTimer = 0.0f; PlayShootSound (); // play shoot sound } public void Reload() { //If bullet left is less than or equal to zero, then YOU SHALL NOT PASS! if (bulletsLeft <= 0) return; int bulletsToLoad = bulletsPerMag - currentBullets; // Bullets to load in new mag // IF Then Else int bulletsToDeduct = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft; bulletsLeft -= bulletsToDeduct; currentBullets += bulletsToDeduct; } private void DoReload() { AnimatorStateInfo info = anim.GetCurrentAnimatorStateInfo (0); if (isReloading) // if its already playing reload, then GET OUTTA HERE! return; anim.CrossFadeInFixedTime ("reload", 0.01f); } private void PlayShootSound() { //_AudioSource.clip = shootSound; _AudioSource.PlayOneShot(shootSound); // play shoot sound } } END OF SCRIPT using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.FirstPerson; public class AimDownSights : MonoBehaviour { public float defaultFOV = 60.0F; public float aimedFOV = 45.0F; public float smoothFOV = 10.0F; public FirstPersonController controller; public Vector3 hipPosition; public Vector3 aimPosition; public float smoothAim = 12.5F; void Update() { if (Input.GetMouseButton(1)) { transform.localPosition = Vector3.Lerp(transform.localPosition, aimPosition, Time.deltaTime * smoothAim); Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, aimedFOV, Time.deltaTime * smoothFOV); controller.m_WalkSpeed = (3); controller.m_RunSpeed = (3); } else { transform.localPosition = Vector3.Lerp(transform.localPosition, hipPosition, Time.deltaTime * smoothAim); Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, defaultFOV, Time.deltaTime * smoothFOV); controller.m_WalkSpeed = (5); controller.m_RunSpeed = (10); } } } END OF SCRIPT #region NAMESPACES using UnityEngine; using System.Collections; #endregion #region WeaponSway Class public class WeaponSway : MonoBehaviour { // PRIVATE BUT EDITABLE IN THE INSPECTOR // IF YOU WANT TO EDIT THESE EXTERNALLY THE USE PROPERTY ACCESSOR [SerializeField] [Range(0.1f, 5f)] [Tooltip("Tilt angle smoothing")] private float _smoothGun = 2f; [SerializeField] [Range(0.1f, 90f)] [Tooltip("Strafing tilt angle")] private float _tiltAngle = 4.25f; [SerializeField] [Range(0.01f, 0.1f)] [Tooltip("Minimum amount of movement")] private float _amount = 0.025f; [SerializeField] [Range(0.01f, 0.1f)] [Tooltip("Minimum amount of movement")] private float _maxAmount = 0.075f; [SerializeField] [Range(0.1f, 5f)] [Tooltip("Directional sway smoothing")] private float _smooth = 2.75f; [SerializeField] [Range(0.1f, 5f)] [Tooltip("Rotational sway smoothing")] private float _smoothRotation = 1.5f; // EXAMPLE PROPERTY ACCESSOR public float tiltAngle { get // USE THIS TO READ THE VALUE - CAN BE REMOVED { return _tiltAngle; } set // USE THIS TO ASSIGN A VALUE - CAN BE REMOVED TO // MAKE READ ONLY WITH GET STILL ABOVE { _tiltAngle = value; } } // PRIVATE SCRIPT ONLY VARIABLES private float _factorX; private float _factorY; private float _tiltGun; private float _tiltZ; private float _tiltX; private Quaternion _target; private Quaternion _target2; private Vector3 _finalPos; private Vector3 _startingPos; void Start() { // ON START WE NEED TO STORE OUR CURRENT LOCAL POSITION _startingPos = transform.localPosition; } void Update() { // DONT NEED TO RETRIVE THIS VALUE UNLESS WE ARE MOVING IN THAT DIRECTION if (Input.GetAxisRaw("Horizontal") > 0 || Input.GetAxisRaw("Horizontal") < 0) { // GET OUR INPUT VALUE AND STORE IT _tiltGun = Input.GetAxisRaw("Horizontal") * _tiltAngle; // SAVE IT AS A ROTATIONAL VALUE _target2 = Quaternion.Euler(0, 0, _tiltGun); // THEN APPLY IT USING SLERP, LOWER THE SMOOTHING THE FASTER THE TRANSISTION WILL BE transform.localRotation = Quaternion.Slerp(transform.localRotation, // STARTING ROTATION _target2, // TARGET ROTATION Time.deltaTime * _smoothGun); // TIME TO GET THERE } // GET OUR AXIS FOR POSITION _factorX = -Input.GetAxisRaw("Mouse X") * _amount; _factorY = -Input.GetAxisRaw("Mouse Y") * _amount; // GET OUR AXIS AGAIN FOR ROTATION _tiltZ = Input.GetAxis("Mouse X") * _tiltAngle; _tiltX = Input.GetAxis("Mouse Y") * _tiltAngle; // CLAMP THEM BY A CERTIAN RANGE TO PREVENT GLITCHING - BOTH AXIS Clamp(_factorX, -_maxAmount, _maxAmount); Clamp(_factorY, -_maxAmount, _maxAmount); // TO GET OUR FINAL SWAY POSITION LERP BETWEEN OUR STARTING AND THE AMOUNT OF NEW MOVEMENT WE DID _finalPos = new Vector3(_startingPos.x + _factorX, // ADD OUT X MOVEMENT TO OUR X AXIS _startingPos.y + _factorY, // ADD OUR Y MOVEMENT TO OUR Y AXIS _startingPos.z); // THIS IS 0 UNLESS ITS MODIFIED ELSEWHERE // LERP BETWEEN STARTING POSITION AND FINAL POSITION BY OUR SMOOTHING OVER TIME transform.localPosition = Vector3.Lerp(transform.localPosition, // WHERE WE STARTED _finalPos, // WHERE WE WANT TO GO Time.deltaTime * _smooth); // BY HOW FAST // STORE OUR TARGET IN A VARIABLE _target = Quaternion.Euler(_tiltX, // AMOUNT OF X ROTATION 0, // AMOUNT OF Y ROTATION _tiltZ); // AMOUNT OF Z ROTATION // THEN FINALLY APPLY OUR ROTATION transform.localRotation = Quaternion.Slerp(transform.localRotation, // STARTING ROATAION _target, // WHERE WE WANT TO GO Time.deltaTime * _smoothRotation); // HOW LONG TO GET THERE } /// /// STATIC METHOD CAN BE CALLED FROM ANYWHERE OUTSIDE THIS SCRIPT BY USING /// WeaponSway.Clamp(amount, min, max); /// /// Value you want to restrct /// Minimum value /// Maximum Value /// public static float Clamp(float amount, float min, float max) { // BIND INPUT AMOUNT TO RANGE BETWEEN MIN AND MAX return Mathf.Clamp(amount, min, max); } } #endregion

Viewing all articles
Browse latest Browse all 592

Trending Articles