- Home /
Question by
beholdenspore28 · Jul 15, 2015 at 10:53 AM ·
animationnot workingshootreload
This animation/shooting script not working. I need some general advise on why.
The reload animation on line 100 of this code doesnt work :(
heres my script:
using UnityEngine; using System.Collections; [RequireComponent(typeof(AudioSource))] [RequireComponent(typeof(Transform))]
public class Shoot : MonoBehaviour {
public Rigidbody Projectile;
public Transform ProjectileSpawn;
public float ProjectileSpeed = 900;
private float BeenShotFor = 0;
public float RecoilAmmount = 0.1f;
private float RecoilToRecover = 0;
private bool Shot = false;
public AudioClip GunSound;
public Transform RecoilCam;
public Transform GunPos;
public Transform AimLocation;
public Transform AimReturnPos;
public float BulletsInMag;
public float MaxBulletsInMag = 30;
public int Magazines = 5;
public float ReloadTime = 5;
private bool Reloading = false;
private bool Sprinting;
void Start (){
BulletsInMag = MaxBulletsInMag;
}
// Update is called once per frame
void Update () {
Reload ();
if (Input.GetKey (KeyCode.LeftShift)) {
Sprinting = true;
} else {
Sprinting = false;
}
if (Input.GetKeyDown("r")){
Reloading = true;
}
if (Shot == true) {
BeenShotFor = BeenShotFor + 1 * Time.deltaTime;
}
if (BeenShotFor >= 0.1){
Shot = false;
BeenShotFor = 0;
}
if (RecoilToRecover >= 0.01f){
GunPos.Translate (Vector3.forward * RecoilToRecover * 10 * Time.deltaTime);
GunPos.Rotate (Vector3.left * RecoilToRecover * 10 * Time.deltaTime);
RecoilToRecover = RecoilToRecover - RecoilToRecover * 10 * Time.deltaTime;
}
if (Sprinting == false){
if (Reloading == false){
if (Shot == false){
if (BulletsInMag > 0){
if (Input.GetButton("Fire1")){
Projectile = Instantiate(Projectile, ProjectileSpawn.position, ProjectileSpawn.rotation) as Rigidbody;
Projectile.AddForce(ProjectileSpawn.position * ProjectileSpeed);
Shot = true;
BulletsInMag--;
if (RecoilToRecover >= 0.5f){
RecoilToRecover = RecoilToRecover - 0.1f;
}
//Sound
GetComponent<AudioSource>().PlayOneShot(GunSound);
//Recoil
GunPos.Translate(Vector3.back * RecoilAmmount * Time.deltaTime);
GunPos.Rotate(Vector3.right * RecoilAmmount * Time.deltaTime);
RecoilToRecover = RecoilToRecover + RecoilAmmount * Time.deltaTime;
}
}
if (Input.GetButton("Fire2")) {
GunPos.position = Vector3.MoveTowards(GunPos.position, AimLocation.position, 0.3f * Time.deltaTime);
} else {
GunPos.position = Vector3.MoveTowards(GunPos.position, AimReturnPos.position, 0.3f * Time.deltaTime);
}
}
}
}
}
void Reload (){
if (Magazines > 0){
if (Reloading == true) {
gameObject.GetComponent<Animation> ().Play ("Reload");
if (ReloadTime < 0){
ReloadTime = 5;
Magazines--;
BulletsInMag = MaxBulletsInMag;
Reloading = false;
}
ReloadTime = ReloadTime - 1 * Time.deltaTime;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Wierd rotations in very simple reload animation 0 Answers
Mecanim restricting animations problem 0 Answers
Animation messes up if GameObject is destroyed 1 Answer
Animation Plays in the Scene but not in the Game Preview 0 Answers
Can't find Mecanim? 2 Answers