From ffd1d5af496e0a0eff343b27c4f0f965bbbf79eb Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 31 Aug 2021 19:07:21 +0800 Subject: *projectile --- .../WebDemo/scripts/CameraShake.cs | 24 ++ .../WebDemo/scripts/CameraShake.cs.meta | 13 ++ .../WebDemo/scripts/ExplodingProjectile.cs | 142 ++++++++++++ .../WebDemo/scripts/ExplodingProjectile.cs.meta | 12 + .../WebDemo/scripts/Projectile.cs | 102 +++++++++ .../WebDemo/scripts/Projectile.cs.meta | 12 + .../WebDemo/scripts/projectileActor.cs | 253 +++++++++++++++++++++ .../WebDemo/scripts/projectileActor.cs.meta | 12 + 8 files changed, 570 insertions(+) create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs.meta create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs.meta create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs.meta create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs create mode 100644 Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs.meta (limited to 'Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts') diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs new file mode 100644 index 00000000..7ea397bf --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CameraShake : MonoBehaviour { + + //public bool cameraShakeBool = true; + public Animator CamerShakeAnimator; + + // Use this for initialization + void Start () { + + } + + // Update is called once per frame + void Update () { + + } + + public void ShakeCamera() + { + CamerShakeAnimator.SetTrigger("CameraShakeTrigger"); + } +} diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs.meta b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs.meta new file mode 100644 index 00000000..8824141a --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/CameraShake.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 8fd6dcdcf07bb644abde2fade0426568 +timeCreated: 1512820034 +licenseType: Store +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs new file mode 100644 index 00000000..ee62553b --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs @@ -0,0 +1,142 @@ +using UnityEngine; +using System.Collections; + +/* THIS CODE IS JUST FOR PREVIEW AND TESTING */ +// Feel free to use any code and picking on it, I cannot guaratnee it will fit into your project +public class ExplodingProjectile : MonoBehaviour +{ + public GameObject impactPrefab; + public GameObject explosionPrefab; + public float thrust; + + public Rigidbody thisRigidbody; + + public GameObject particleKillGroup; + private Collider thisCollider; + + public bool LookRotation = true; + public bool Missile = false; + public Transform missileTarget; + public float projectileSpeed; + public float projectileSpeedMultiplier; + + public bool ignorePrevRotation = false; + + public bool explodeOnTimer = false; + public float explosionTimer; + float timer; + + private Vector3 previousPosition; + + // Use this for initialization + void Start() + { + thisRigidbody = GetComponent(); + if (Missile) + { + missileTarget = GameObject.FindWithTag("Target").transform; + } + thisCollider = GetComponent(); + previousPosition = transform.position; + } + + // Update is called once per frame + void Update() + { + /* if(Input.GetButtonUp("Fire2")) + { + Explode(); + }*/ + timer += Time.deltaTime; + if (timer >= explosionTimer && explodeOnTimer == true) + { + Explode(); + } + + } + + void FixedUpdate() + { + if (Missile) + { + projectileSpeed += projectileSpeed * projectileSpeedMultiplier; + // transform.position = Vector3.MoveTowards(transform.position, missileTarget.transform.position, 0); + + transform.LookAt(missileTarget); + + thisRigidbody.AddForce(transform.forward * projectileSpeed); + } + + if (LookRotation && timer >= 0.05f) + { + transform.rotation = Quaternion.LookRotation(thisRigidbody.velocity); + } + + CheckCollision(previousPosition); + + previousPosition = transform.position; + } + + void CheckCollision(Vector3 prevPos) + { + RaycastHit hit; + Vector3 direction = transform.position - prevPos; + Ray ray = new Ray(prevPos, direction); + float dist = Vector3.Distance(transform.position, prevPos); + if (Physics.Raycast(ray, out hit, dist)) + { + transform.position = hit.point; + Quaternion rot = Quaternion.FromToRotation(Vector3.forward, hit.normal); + Vector3 pos = hit.point; + Instantiate(impactPrefab, pos, rot); + if (!explodeOnTimer && Missile == false) + { + Destroy(gameObject); + } + else if (Missile == true) + { + thisCollider.enabled = false; + particleKillGroup.SetActive(false); + thisRigidbody.velocity = Vector3.zero; + Destroy(gameObject, 5); + } + + } + } + + void OnCollisionEnter(Collision collision) + { + if (collision.gameObject.tag != "FX") + { + ContactPoint contact = collision.contacts[0]; + Quaternion rot = Quaternion.FromToRotation(Vector3.forward, contact.normal); + if (ignorePrevRotation) + { + rot = Quaternion.Euler(0, 0, 0); + } + Vector3 pos = contact.point; + Instantiate(impactPrefab, pos, rot); + if (!explodeOnTimer && Missile == false) + { + Destroy(gameObject); + } + else if (Missile == true) + { + + thisCollider.enabled = false; + particleKillGroup.SetActive(false); + thisRigidbody.velocity = Vector3.zero; + + Destroy(gameObject, 5); + + } + } + } + + void Explode() + { + Instantiate(explosionPrefab, gameObject.transform.position, Quaternion.Euler(0, 0, 0)); + Destroy(gameObject); + } + +} \ No newline at end of file diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs.meta b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs.meta new file mode 100644 index 00000000..c912e350 --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/ExplodingProjectile.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 538d28f745155f34fba977c5c1026e39 +timeCreated: 1483283774 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs new file mode 100644 index 00000000..69c939c6 --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs @@ -0,0 +1,102 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StylizedProjectile +{ + + +public class Projectile : MonoBehaviour +{ + private float disappearAfterTime = 3; + private float disappearTimer = 0; + + private bool isActive = false; + + // private MeshRenderer meshRenderer; + + private Vector3 direction; + private Vector3 startPosition; + + private float movementSpeed = 1; + + private Vector3 targetPosition; + + private float distanceToTarget; + private float movementValue; + + // Use this for initialization + void Start () + { + // We cache this for performance reasons, GetComponent is slow to do realtime + // meshRenderer = GetComponent(); + // meshRenderer = GetComponent(); + } + + // Update is called once per frame + void Update () { + if (isActive) // Only update stuff if we're alive + { + disappearTimer += Time.deltaTime; // Increase disappear timer + if (disappearTimer > disappearAfterTime) // If we're alive too long, get rekt + { + disappearTimer = 0; // Reset timer + isActive = false; // Is not active anymore + // meshRenderer.enabled = false; // Disable meshrender so it's invisible + } + + + // 1/distanceToTarget is the calculation to move 1 unit per second, movementspeed defines how many units per second you want to move + movementValue += (1/distanceToTarget*movementSpeed) * Time.deltaTime; + if (movementValue > 1) + { + movementValue = 1; + Explode(); + } + Move(); + + } + } + + void Move() + { + // lerp goes from 0 to 1, 0 is startPosition, 1 is the targets position; + transform.position = Vector3.Lerp(startPosition, targetPosition, movementValue); + } + + void MoveWithoutTargetHit() + { + transform.position += direction.normalized * movementSpeed; + } + + public void Fire(Vector3 target, Vector3 spawnPosition, Vector3 Direction, float speed) + { + if (isActive) // If we're active, just return so we don't execute any code + return; + + isActive = true; // Set active + disappearTimer = 0; // Reset timer just in case it's not reset + transform.position = spawnPosition; // set spawn position + // meshRenderer.enabled = true; // Enable meshrender so it's visible + movementSpeed = speed; // Units per second + direction = Direction.normalized; // Normalize the direction + targetPosition = target; // Set target transform - Can be null for continous movement without target + distanceToTarget = Vector3.Distance(targetPosition, transform.position); + startPosition = spawnPosition; + movementValue = 0; + } + + // It gets here after it hits something + void Explode() + { + disappearTimer = 0; // Reset timer + isActive = false; // Is not active anymore + // meshRenderer.enabled = false; // Disable meshrender so it's invisible + } + + public bool GetIsActive() + { + return isActive; + } +} +} \ No newline at end of file diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs.meta b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs.meta new file mode 100644 index 00000000..e7b30e7a --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/Projectile.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 989960b1a1aad4b449f0051bdeb3d79f +timeCreated: 1485807647 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs new file mode 100644 index 00000000..86fc9fea --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs @@ -0,0 +1,253 @@ +using UnityEngine; +using System.Collections; +using UnityEngine.UI; + +public class projectileActor : MonoBehaviour { + + public Transform spawnLocator; + public Transform spawnLocatorMuzzleFlare; + public Transform shellLocator; + public Animator recoilAnimator; + + public Transform[] shotgunLocator; + + [System.Serializable] + public class projectile + { + public string name; + public Rigidbody bombPrefab; + public GameObject muzzleflare; + public float min, max; + public bool rapidFire; + public float rapidFireCooldown; + + public bool shotgunBehavior; + public int shotgunPellets; + public GameObject shellPrefab; + public bool hasShells; + } + public projectile[] bombList; + + + string FauxName; + public Text UiText; + + public bool UImaster = true; + public bool CameraShake = true; + public float rapidFireDelay; + public CameraShake CameraShakeCaller; + + float firingTimer; + public bool firing; + public int bombType = 0; + + // public ParticleSystem muzzleflare; + + public bool swarmMissileLauncher = false; + int projectileSimFire = 1; + + public bool Torque = false; + public float Tor_min, Tor_max; + + public bool MinorRotate; + public bool MajorRotate = false; + int seq = 0; + + + // Use this for initialization + void Start () + { + if (UImaster) + { + UiText.text = bombList[bombType].name.ToString(); + } + if (swarmMissileLauncher) + { + projectileSimFire = 5; + } + } + + // Update is called once per frame + void Update () + { + //Movement + if(Input.GetButton("Horizontal")) + { + if (Input.GetAxis("Horizontal") < 0) + { + gameObject.transform.Rotate(Vector3.up, -25 * Time.deltaTime); + } + else + { + gameObject.transform.Rotate(Vector3.up, 25 * Time.deltaTime); + } + } + + //BULLETS + if (Input.GetKeyDown(KeyCode.D)) + { + Switch(-1); + } + if (Input.GetButtonDown("Fire2") || Input.GetKeyDown(KeyCode.A)) + { + Switch(1); + } + + if(Input.GetButtonDown("Fire1")) + { + firing = true; + Fire(); + } + if (Input.GetButtonUp("Fire1")) + { + firing = false; + firingTimer = 0; + } + + if (bombList[bombType].rapidFire && firing) + { + if(firingTimer > bombList[bombType].rapidFireCooldown+rapidFireDelay) + { + Fire(); + firingTimer = 0; + } + } + + if(firing) + { + firingTimer += Time.deltaTime; + } + } + + public void Switch(int value) + { + bombType += value; + if (bombType < 0) + { + bombType = bombList.Length; + bombType--; + } + else if (bombType >= bombList.Length) + { + bombType = 0; + } + if (UImaster) + { + UiText.text = bombList[bombType].name.ToString(); + } + } + + public void Fire() + { + if(CameraShake) + { + CameraShakeCaller.ShakeCamera(); + } + Instantiate(bombList[bombType].muzzleflare, spawnLocatorMuzzleFlare.position, spawnLocatorMuzzleFlare.rotation); + // bombList[bombType].muzzleflare.Play(); + + if (bombList[bombType].hasShells) + { + Instantiate(bombList[bombType].shellPrefab, shellLocator.position, shellLocator.rotation); + } + recoilAnimator.SetTrigger("recoil_trigger"); + + Rigidbody rocketInstance; + rocketInstance = Instantiate(bombList[bombType].bombPrefab, spawnLocator.position,spawnLocator.rotation) as Rigidbody; + // Quaternion.Euler(0,90,0) + rocketInstance.AddForce(spawnLocator.forward * Random.Range(bombList[bombType].min, bombList[bombType].max)); + + if (bombList[bombType].shotgunBehavior) + { + for(int i = 0; i < bombList[bombType].shotgunPellets ;i++ ) + { + Rigidbody rocketInstanceShotgun; + rocketInstanceShotgun = Instantiate(bombList[bombType].bombPrefab, shotgunLocator[i].position, shotgunLocator[i].rotation) as Rigidbody; + // Quaternion.Euler(0,90,0) + rocketInstanceShotgun.AddForce(shotgunLocator[i].forward * Random.Range(bombList[bombType].min, bombList[bombType].max)); + } + } + + if (Torque) + { + rocketInstance.AddTorque(spawnLocator.up * Random.Range(Tor_min, Tor_max)); + } + if (MinorRotate) + { + RandomizeRotation(); + } + if (MajorRotate) + { + Major_RandomizeRotation(); + } + } + + + void RandomizeRotation() + { + if (seq == 0) + { + seq++; + transform.Rotate(0, 1, 0); + } + else if (seq == 1) + { + seq++; + transform.Rotate(1, 1, 0); + } + else if (seq == 2) + { + seq++; + transform.Rotate(1, -3, 0); + } + else if (seq == 3) + { + seq++; + transform.Rotate(-2, 1, 0); + } + else if (seq == 4) + { + seq++; + transform.Rotate(1, 1, 1); + } + else if (seq == 5) + { + seq = 0; + transform.Rotate(-1, -1, -1); + } + } + + void Major_RandomizeRotation() + { + if (seq == 0) + { + seq++; + transform.Rotate(0, 25, 0); + } + else if (seq == 1) + { + seq++; + transform.Rotate(0, -50, 0); + } + else if (seq == 2) + { + seq++; + transform.Rotate(0, 25, 0); + } + else if (seq == 3) + { + seq++; + transform.Rotate(25, 0, 0); + } + else if (seq == 4) + { + seq++; + transform.Rotate(-50, 0, 0); + } + else if (seq == 5) + { + seq = 0; + transform.Rotate(25, 0, 0); + } + } +} diff --git a/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs.meta b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs.meta new file mode 100644 index 00000000..682e1742 --- /dev/null +++ b/Assets/Art/Vfx/StylizedProjectilePack1/WebDemo/scripts/projectileActor.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 24ae8ab904635734bab03b361009b30d +timeCreated: 1483283782 +licenseType: Store +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.1-26-g67d0