Guest User

Jetpack Kurt ballstic bullet flight Unity3D

a guest
Aug 3rd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.16 KB | None | 0 0
  1. /*
  2.     The following license supersedes all notices in the source code.
  3.  
  4.     Copyright (c) 2020 Kurt Dekker/PLBM Games All rights reserved.
  5.  
  6.     http://www.twitter.com/kurtdekker
  7.  
  8.     Redistribution and use in source and binary forms, with or without
  9.     modification, are permitted provided that the following conditions are
  10.     met:
  11.  
  12.     Redistributions of source code must retain the above copyright notice,
  13.     this list of conditions and the following disclaimer.
  14.  
  15.     Redistributions in binary form must reproduce the above copyright
  16.     notice, this list of conditions and the following disclaimer in the
  17.     documentation and/or other materials provided with the distribution.
  18.  
  19.     Neither the name of the Kurt Dekker/PLBM Games nor the names of its
  20.     contributors may be used to endorse or promote products derived from
  21.     this software without specific prior written permission.
  22.  
  23.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24.     IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25.     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26.     PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27.     HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28.     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  29.     TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30.     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31.     LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32.     NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33.     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35.  
  36. // This is part of Jetpack Kurt Space Flight, by Kurt Dekker, available here:
  37. //
  38. // Appstore: https://itunes.apple.com/us/app/jetpack-kurt/id1033348911
  39. // GooglePlay: https://play.google.com/store/apps/details?id=com.plbm.jetpack
  40. //
  41. // External TestFlight link: https://testflight.apple.com/join/wt0HTUba
  42. //
  43. // Youtube playlist: https://www.youtube.com/playlist?list=PLTzufax_A179M51bQgMFDJ7y9Gjp9kOVR
  44. //
  45. // Itch.io: https://kurtdekker.itch.io/jetpack
  46. //
  47. // Simmer.io: https://simmer.io/@kurtdekker/jetpack-kurt
  48.  
  49.  
  50. using System.Collections;
  51. using System.Collections.Generic;
  52. using UnityEngine;
  53.  
  54. public class WeaponShot1Controller : MonoBehaviour
  55. {
  56.     const float LaserBoltSpeed = 150.0f;
  57.  
  58.     const float Gravity = -10.0f;
  59.  
  60.     Vector3 velocity;
  61.  
  62.     bool reactive = true;
  63.  
  64.     public static WeaponShot1Controller Create(
  65.         Vector3 startPosition,
  66.         Vector3 startForward,
  67.         Vector3 playerVelocity)
  68.     {
  69.         var sc = new GameObject( "WeaponShot1Controller.Create();").AddComponent<WeaponShot1Controller>();
  70.  
  71.         TTL.Attach( sc.gameObject, Random.Range( 3.5f, 4.5f));
  72.  
  73.         sc.transform.position = startPosition;
  74.  
  75.         Instantiate<GameObject>( Resources.Load<GameObject>(
  76.             "Prefabs/WeaponShot1Controller_prefab"), sc.transform).GetComponent<ParticleSystem>();
  77.  
  78.         sc.velocity = playerVelocity + startForward.normalized * LaserBoltSpeed;
  79.  
  80.         sc.UpdateMovement();
  81.  
  82.         return sc;
  83.     }
  84.  
  85.     void UpdateMovement()
  86.     {
  87.         velocity += Vector3.up * Gravity * Time.deltaTime;
  88.  
  89.         Vector3 movement = velocity * Time.deltaTime;
  90.  
  91.         Vector3 newPosition = transform.position + movement;
  92.  
  93.         // possible outcomes from impact/contact with a collider:
  94.         //
  95.         //  - don't ricochet off a damage-taker (this might not be necessary)
  96.         //
  97.         //  - case of ricochet:
  98.         //      - choose random speed attenuation
  99.         //      - deflect according to normal
  100.         //      - deflect a bit more randomly
  101.         //      - if below a certain amount, become unreactive
  102.         //
  103.         //  - case of impact and finish:
  104.         //      - destroy
  105.         //
  106.         // If reactive, leave a little splash cloud of particles
  107.         //
  108.         Ray ray = new Ray( transform.position, velocity);
  109.         RaycastHit rch;
  110.         if (Physics.Raycast( ray, out rch, movement.magnitude, ~(1 << MyLayers.i_Player)))
  111.         {
  112.             bool CanRicochet = reactive;
  113.  
  114.             newPosition = rch.point;
  115.  
  116.             var da = rch.collider.GetComponent<ICombatDamageable>();
  117.             if (da != null)
  118.             {
  119.                 da.TakeDamage( 1.0f);
  120.  
  121.                 CanRicochet = false;
  122.  
  123.                 STATE.MissionShotsHit++;
  124.             }
  125.  
  126.             if (reactive)
  127.             {
  128.                 SpaceFlightShotImpact.Create( newPosition, rch.normal);
  129.             }
  130.  
  131.             // chance of carom/ricochet?
  132.             if (CanRicochet)
  133.             {
  134.                 if (Random.Range( 0, 6) == 0)
  135.                 {
  136.                     float attenuate1 = Random.Range( 0.05f, 0.6f);
  137.                     float attenuate2 = Mathf.Min( attenuate1, Random.Range( 0.05f, 0.6f));
  138.                     float attenuate3 = Mathf.Min( attenuate2, Random.Range( 0.05f, 0.6f));
  139.  
  140.                     velocity.x *= attenuate3;
  141.                     velocity.y *= attenuate1;
  142.                     velocity.z *= attenuate3;
  143.  
  144.                     var impactSpeed = Vector3.Dot( velocity, rch.normal);
  145.  
  146.                     var reflect = rch.normal * impactSpeed * Random.Range( 1.8f, 3.0f);
  147.  
  148.                     velocity -= reflect;
  149.  
  150.                     // chance of becoming unreactive: we no longer make ricochet splash visuals
  151.                     if (velocity.magnitude < 30)
  152.                     {
  153.                         // and a sub-chance of just going away when we're this slow
  154.                         if (Random.Range( 0, 3) > 0)
  155.                         {
  156.                             reactive = false;
  157.                         }
  158.                     }
  159.                 }
  160.             }
  161.             else
  162.             {
  163.                 Destroy( gameObject);
  164.             }
  165.         }
  166.  
  167.         Vector3 oldPosition = transform.position;
  168.  
  169.         transform.position = newPosition;
  170.     }
  171.  
  172.     void Update ()
  173.     {
  174.         UpdateMovement();
  175.     }
  176. }
  177.  
Add Comment
Please, Sign In to add comment