Search Unity

need help. I have been at it for a few days (Solved)

Discussion in 'Scripting' started by keepthachange, Feb 7, 2020.

  1. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    Trying to figure this out?

    I'm not sure where to put this? I have been trying for a few days now?


    Where the TopDown Engine's damage system references which target is hit, you would use:

    Code (CSharp):
    1. Damage (int DamageAmount, TargetType AttackersTargetType, Transform YourTargetsTransform, int RagdollForce)
    So, something like this:

    Code (CSharp):
    1. if (YourTargetReference.GetComponent<EmeraldAI.EmeraldAISystem>() != null)
    2. {
    3.    YourTargetReference.GetComponent<EmeraldAI.EmeraldAISystem>().Damage(YourDamageAmount, EmeraldAI.EmeraldAISystem.TargetType.Player, YourPlayerTransform, 400);
    4. }


    this is top downs damage script it attached to projectile

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using MoreMountains.Tools;
    4. using System.Collections.Generic;
    5. using System;
    6. using MoreMountains.Feedbacks;
    7.  
    8. namespace MoreMountains.TopDownEngine
    9. {
    10.     /// <summary>
    11.     /// Add this component to an object and it will cause damage to objects that collide with it.
    12.     /// </summary>
    13.     [AddComponentMenu("TopDown Engine/Character/Damage/DamageOnTouch")]
    14.     public class DamageOnTouch : MonoBehaviour
    15.     {
    16.         /// the possible ways to add knockback : noKnockback, which won't do nothing, set force, or add force
    17.         public enum KnockbackStyles { NoKnockback, AddForce }
    18.         /// the possible knockback directions
    19.         public enum KnockbackDirections { BasedOnOwnerPosition, BasedOnSpeed }
    20.  
    21.         [Header("Targets")]
    22.         [Information("This component will make your object cause damage to objects that collide with it. Here you can define what layers will be affected by the damage (for a standard enemy, choose Player), how much damage to give, and how much force should be applied to the object that gets the damage on hit. You can also specify how long the post-hit invincibility should last (in seconds).", MoreMountains.Tools.InformationAttribute.InformationType.Info, false)]
    23.         // the layers that will be damaged by this object
    24.         public LayerMask TargetLayerMask;
    25.         /// set this to true to have your object teleport to the impact point on death. Useful for fast moving stuff like projectiles.
    26.         public bool PerfectImpact = false;
    27.  
    28.         [Header("Damage Caused")]
    29.         /// The amount of health to remove from the player's health
    30.         public int DamageCaused = 10;
    31.         /// the type of knockback to apply when causing damage
    32.         public KnockbackStyles DamageCausedKnockbackType = KnockbackStyles.AddForce;
    33.         /// The direction to apply the knockback
    34.         public KnockbackDirections DamageCausedKnockbackDirection;
    35.         /// The force to apply to the object that gets damaged
    36.         public Vector3 DamageCausedKnockbackForce = new Vector3(10, 10, 0);
    37.         /// The duration of the invincibility frames after the hit (in seconds)
    38.         public float InvincibilityDuration = 0.5f;
    39.  
    40.         [Header("Damage Taken")]
    41.         [Information("After having applied the damage to whatever it collided with, you can have this object hurt itself. A bullet will explode after hitting a wall for example. Here you can define how much damage it'll take every time it hits something, or only when hitting something that's damageable, or non damageable. Note that this object will need a Health component too for this to be useful.", MoreMountains.Tools.InformationAttribute.InformationType.Info, false)]
    42.         /// The amount of damage taken every time, whether what we collide with is damageable or not
    43.         public int DamageTakenEveryTime = 0;
    44.         /// The amount of damage taken when colliding with a damageable object
    45.         public int DamageTakenDamageable = 0;
    46.         /// The amount of damage taken when colliding with something that is not damageable
    47.         public int DamageTakenNonDamageable = 0;
    48.         /// the type of knockback to apply when taking damage
    49.         public KnockbackStyles DamageTakenKnockbackType = KnockbackStyles.NoKnockback;
    50.         /// The direction to apply the knockback
    51.         public KnockbackDirections DamagedTakenKnockbackDirection;
    52.         /// The force to apply to the object that gets damaged
    53.         public Vector3 DamageTakenKnockbackForce = Vector3.zero;
    54.         /// The duration of the invincibility frames after the hit (in seconds)
    55.         public float DamageTakenInvincibilityDuration = 0.5f;
    56.  
    57.         [Header("Feedbacks")]
    58.         public MMFeedbacks HitDamageableFeedback;
    59.         public MMFeedbacks HitNonDamageableFeedback;
    60.  
    61.         [ReadOnly]
    62.         /// the owner of the DamageOnTouch zone
    63.         public GameObject Owner;
    64.  
    65.         // storage      
    66.         protected Vector3 _lastPosition, _velocity, _knockbackForce;
    67.         protected float _startTime = 0f;
    68.         protected Health _colliderHealth;
    69.         protected TopDownController _topDownController;
    70.         protected TopDownController _colliderTopDownController;
    71.         protected Rigidbody _colliderRigidBody;
    72.         protected Health _health;
    73.         protected List<GameObject> _ignoredGameObjects;
    74.         protected Vector3 _collisionPoint;
    75.         protected Vector3 _knockbackForceApplied;
    76.         protected CircleCollider2D _circleCollider2D;
    77.         protected BoxCollider2D _boxCollider2D;
    78.         protected SphereCollider _sphereCollider;
    79.         protected BoxCollider _boxCollider;
    80.         protected Color _gizmosColor;
    81.         protected Vector3 _gizmoSize;
    82.         protected Vector3 _gizmoOffset;
    83.         protected Transform _gizmoTransform;
    84.  
    85.         /// <summary>
    86.         /// Initialization
    87.         /// </summary>
    88.         protected virtual void Awake()
    89.         {
    90.             _ignoredGameObjects = new List<GameObject>();
    91.             _health = GetComponent<Health>();
    92.             _topDownController = GetComponent<TopDownController>();
    93.             _boxCollider2D = GetComponent<BoxCollider2D>();
    94.             _boxCollider = GetComponent<BoxCollider>();
    95.             _sphereCollider = GetComponent<SphereCollider>();
    96.             _circleCollider2D = GetComponent<CircleCollider2D>();
    97.            
    98.             _gizmosColor = Color.red;
    99.             _gizmosColor.a = 0.25f;
    100.  
    101.             InitializeFeedbacks();
    102.         }
    103.  
    104.         public virtual void InitializeFeedbacks()
    105.         {
    106.             HitDamageableFeedback?.Initialization(this.gameObject);
    107.             HitNonDamageableFeedback?.Initialization(this.gameObject);
    108.         }
    109.  
    110.         public virtual void SetGizmoSize(Vector3 newGizmoSize)
    111.         {
    112.             _boxCollider2D = GetComponent<BoxCollider2D>();
    113.             _boxCollider = GetComponent<BoxCollider>();
    114.             _sphereCollider = GetComponent<SphereCollider>();
    115.             _circleCollider2D = GetComponent<CircleCollider2D>();
    116.             _gizmoSize = newGizmoSize;
    117.         }
    118.  
    119.         public virtual void SetGizmoOffset(Vector3 newOffset)
    120.         {
    121.             _gizmoOffset = newOffset;
    122.         }
    123.  
    124.         /// <summary>
    125.         /// OnEnable we set the start time to the current timestamp
    126.         /// </summary>
    127.         protected virtual void OnEnable()
    128.         {
    129.             _startTime = Time.time;
    130.         }
    131.  
    132.         /// <summary>
    133.         /// During last update, we store the position and velocity of the object
    134.         /// </summary>
    135.         protected virtual void Update()
    136.         {
    137.             ComputeVelocity();
    138.         }
    139.  
    140.         /// <summary>
    141.         /// Adds the gameobject set in parameters to the ignore list
    142.         /// </summary>
    143.         /// <param name="newIgnoredGameObject">New ignored game object.</param>
    144.         public virtual void IgnoreGameObject(GameObject newIgnoredGameObject)
    145.         {
    146.             _ignoredGameObjects.Add(newIgnoredGameObject);
    147.         }
    148.  
    149.         /// <summary>
    150.         /// Removes the object set in parameters from the ignore list
    151.         /// </summary>
    152.         /// <param name="ignoredGameObject">Ignored game object.</param>
    153.         public virtual void StopIgnoringObject(GameObject ignoredGameObject)
    154.         {
    155.             _ignoredGameObjects.Remove(ignoredGameObject);
    156.         }
    157.  
    158.         /// <summary>
    159.         /// Clears the ignore list.
    160.         /// </summary>
    161.         public virtual void ClearIgnoreList()
    162.         {
    163.             _ignoredGameObjects.Clear();
    164.         }
    165.  
    166.         /// <summary>
    167.         /// Computes the velocity based on the object's last position
    168.         /// </summary>
    169.         protected virtual void ComputeVelocity()
    170.         {
    171.             _velocity = (_lastPosition - (Vector3)transform.position) / Time.deltaTime;
    172.             _lastPosition = transform.position;
    173.         }
    174.  
    175.         /// <summary>
    176.         /// When a collision with the player is triggered, we give damage to the player and knock it back
    177.         /// </summary>
    178.         /// <param name="collider">what's colliding with the object.</param>
    179.         public virtual void OnTriggerStay2D(Collider2D collider)
    180.         {
    181.             Colliding(collider.gameObject);
    182.         }
    183.  
    184.         /// <summary>
    185.         /// On trigger enter 2D, we call our colliding endpoint
    186.         /// </summary>
    187.         /// <param name="collider"></param>S
    188.         public virtual void OnTriggerEnter2D(Collider2D collider)
    189.         {
    190.             Colliding(collider.gameObject);
    191.         }
    192.  
    193.         /// <summary>
    194.         /// On trigger stay, we call our colliding endpoint
    195.         /// </summary>
    196.         /// <param name="collider"></param>
    197.         public virtual void OnTriggerStay(Collider collider)
    198.         {
    199.             Colliding(collider.gameObject);
    200.         }
    201.  
    202.         /// <summary>
    203.         /// On trigger enter, we call our colliding endpoint
    204.         /// </summary>
    205.         /// <param name="collider"></param>
    206.         public virtual void OnTriggerEnter(Collider collider)
    207.         {
    208.             Colliding(collider.gameObject);
    209.         }
    210.  
    211.         /// <summary>
    212.         /// When colliding, we apply damage
    213.         /// </summary>
    214.         /// <param name="collider"></param>
    215.         protected virtual void Colliding(GameObject collider)
    216.         {
    217.             if (!this.isActiveAndEnabled)
    218.             {
    219.                 return;
    220.             }
    221.  
    222.             // if the object we're colliding with is part of our ignore list, we do nothing and exit
    223.             if (_ignoredGameObjects.Contains(collider))
    224.             {
    225.                 return;
    226.             }
    227.  
    228.             // if what we're colliding with isn't part of the target layers, we do nothing and exit
    229.             if (!MMLayers.LayerInLayerMask(collider.layer, TargetLayerMask))
    230.             {
    231.  
    232.                 return;
    233.             }
    234.  
    235.             // if we're on our first frame, we don't apply damage
    236.             if (Time.time == 0f)
    237.             {
    238.                 return;
    239.             }
    240.  
    241.             _collisionPoint = this.transform.position;
    242.             _colliderHealth = collider.gameObject.MMGetComponentNoAlloc<Health>();
    243.  
    244.             // if what we're colliding with is damageable
    245.             if (_colliderHealth != null)
    246.             {
    247.                 if (_colliderHealth.CurrentHealth > 0)
    248.                 {
    249.                     OnCollideWithDamageable(_colliderHealth);
    250.                 }
    251.             }
    252.  
    253.             // if what we're colliding with can't be damaged
    254.             else
    255.             {
    256.                 OnCollideWithNonDamageable();
    257.             }
    258.         }
    259.  
    260.         /// <summary>
    261.         /// Describes what happens when colliding with a damageable object
    262.         /// </summary>
    263.         /// <param name="health">Health.</param>
    264.         protected virtual void OnCollideWithDamageable(Health health)
    265.         {
    266.             // if what we're colliding with is a TopDownController, we apply a knockback force
    267.             _colliderTopDownController = health.gameObject.MMGetComponentNoAlloc<TopDownController>();
    268.             _colliderRigidBody = health.gameObject.MMGetComponentNoAlloc<Rigidbody>();
    269.  
    270.             if ((_colliderTopDownController != null) && (DamageCausedKnockbackForce != Vector3.zero) && (!_colliderHealth.Invulnerable) && (!_colliderHealth.ImmuneToKnockback))
    271.             {
    272.                 _knockbackForce.x = DamageCausedKnockbackForce.x;
    273.                 _knockbackForce.y = DamageCausedKnockbackForce.y;
    274.  
    275.                 if (DamageCausedKnockbackDirection == KnockbackDirections.BasedOnSpeed)
    276.                 {
    277.                     Vector3 totalVelocity = _colliderTopDownController.Speed + _velocity;
    278.                     _knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, totalVelocity.normalized, 10f, 0f);
    279.                 }
    280.                 if (DamagedTakenKnockbackDirection == KnockbackDirections.BasedOnOwnerPosition)
    281.                 {
    282.                     if (Owner == null) { Owner = this.gameObject; }
    283.                     Vector3 relativePosition = _colliderTopDownController.transform.position - Owner.transform.position;
    284.                     _knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, relativePosition.normalized, 10f, 0f);
    285.                 }
    286.  
    287.                 if (DamageCausedKnockbackType == KnockbackStyles.AddForce)
    288.                 {
    289.                     _colliderTopDownController.Impact(_knockbackForce.normalized, _knockbackForce.magnitude);
    290.                 }
    291.             }
    292.  
    293.             HitDamageableFeedback?.PlayFeedbacks(this.transform.position);
    294.  
    295.             // we apply the damage to the thing we've collided with
    296.             _colliderHealth.Damage(DamageCaused, gameObject, InvincibilityDuration, InvincibilityDuration);
    297.             if (DamageTakenEveryTime + DamageTakenDamageable > 0)
    298.             {
    299.                 SelfDamage(DamageTakenEveryTime + DamageTakenDamageable);
    300.             }
    301.         }
    302.  
    303.         /// <summary>
    304.         /// Describes what happens when colliding with a non damageable object
    305.         /// </summary>
    306.         protected virtual void OnCollideWithNonDamageable()
    307.         {
    308.             if (DamageTakenEveryTime + DamageTakenNonDamageable > 0)
    309.             {
    310.                 SelfDamage(DamageTakenEveryTime + DamageTakenNonDamageable);
    311.             }
    312.  
    313.             HitNonDamageableFeedback?.PlayFeedbacks(this.transform.position);
    314.         }
    315.  
    316.         /// <summary>
    317.         /// Applies damage to itself
    318.         /// </summary>
    319.         /// <param name="damage">Damage.</param>
    320.         protected virtual void SelfDamage(int damage)
    321.         {
    322.             if (_health != null)
    323.             {
    324.                 _health.Damage(damage, gameObject, 0f, DamageTakenInvincibilityDuration);
    325.  
    326.                 if ((_health.CurrentHealth <= 0) && PerfectImpact)
    327.                 {
    328.                     this.transform.position = _collisionPoint;
    329.                 }
    330.             }
    331.  
    332.             // if what we're colliding with is a TopDownController, we apply a knockback force
    333.             if (_topDownController != null)
    334.             {
    335.                 Vector2 totalVelocity = _colliderTopDownController.Speed + _velocity;
    336.                 Vector2 knockbackForce = Vector3.RotateTowards(DamageCausedKnockbackForce, totalVelocity.normalized, 10f, 0f);
    337.  
    338.                 if (DamageTakenKnockbackType == KnockbackStyles.AddForce)
    339.                 {
    340.                     _topDownController.AddForce(knockbackForce);
    341.                 }
    342.             }
    343.         }
    344.  
    345.         /// <summary>
    346.         /// draws a cube or sphere around the damage area
    347.         /// </summary>
    348.         protected virtual void OnDrawGizmos()
    349.         {
    350.             Gizmos.color = _gizmosColor;
    351.  
    352.             if (_boxCollider2D != null)
    353.             {
    354.                 if (_boxCollider2D.enabled)
    355.                 {
    356.                     MMDebug.DrawGizmoCube(this.transform,
    357.                                             _gizmoOffset,
    358.                                             _boxCollider2D.size,
    359.                                             false);
    360.                 }
    361.                 else
    362.                 {
    363.                     MMDebug.DrawGizmoCube(this.transform,
    364.                                             _gizmoOffset,
    365.                                             _boxCollider2D.size,
    366.                                             true);
    367.                 }              
    368.             }
    369.  
    370.             if (_circleCollider2D != null)
    371.             {
    372.                 if (_circleCollider2D.enabled)
    373.                 {
    374.                     Gizmos.DrawSphere((Vector2)this.transform.position + _circleCollider2D.offset, _circleCollider2D.radius);
    375.                 }
    376.                 else
    377.                 {
    378.                     Gizmos.DrawWireSphere((Vector2)this.transform.position + _circleCollider2D.offset, _circleCollider2D.radius);
    379.                 }
    380.             }
    381.            
    382.             if (_boxCollider != null)
    383.             {
    384.                 if (_boxCollider.enabled)
    385.                 {
    386.                     MMDebug.DrawGizmoCube(this.transform,
    387.                                             _gizmoOffset,
    388.                                             _boxCollider.size,
    389.                                             false);
    390.                 }
    391.                 else
    392.                 {
    393.                     MMDebug.DrawGizmoCube(this.transform,
    394.                                             _gizmoOffset,
    395.                                             _boxCollider.size,
    396.                                             true);
    397.                 }
    398.             }
    399.            
    400.             if (_sphereCollider != null)
    401.             {
    402.                 if (_sphereCollider.enabled)
    403.                 {
    404.                     Gizmos.DrawSphere(this.transform.position, _sphereCollider.radius);
    405.                 }
    406.                 else
    407.                 {
    408.                     Gizmos.DrawWireSphere(this.transform.position, _sphereCollider.radius);
    409.                 }              
    410.             }
    411.         }
    412.  
    413.     }
    414. }
    this is the projectile script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using MoreMountains.Tools;
    4.  
    5. namespace MoreMountains.TopDownEngine
    6. {  
    7.     /// <summary>
    8.     /// Projectile class to be used along with projectile weapons
    9.     /// </summary>
    10.     [AddComponentMenu("TopDown Engine/Weapons/Projectile")]
    11.     public class Projectile : MMPoolableObject
    12.     {
    13.         [Header("Movement")]
    14.         /// if true, the projectile will rotate at initialization towards its rotation
    15.         public bool FaceDirection = true;
    16.         /// the speed of the object (relative to the level's speed)
    17.         public float Speed=0;
    18.         /// the acceleration of the object over time. Starts accelerating on enable.
    19.         public float Acceleration = 0;
    20.         /// the current direction of the object
    21.         public Vector3 Direction = Vector3.left;
    22.         /// if set to true, the spawner can change the direction of the object. If not the one set in its inspector will be used.
    23.         public bool DirectionCanBeChangedBySpawner = true;
    24.         /// the flip factor to apply if and when the projectile is mirrored
    25.         public Vector3 FlipValue = new Vector3(-1,1,1);
    26.         /// determines whether or not the projectile is facing right
    27.         public bool ProjectileIsFacingRight = true;
    28.  
    29.         [Header("Spawn")]
    30.         [Information("Here you can define an initial delay (in seconds) during which this object won't take or cause damage. This delay starts when the object gets enabled. You can also define whether the projectiles should damage their owner (think rockets and the likes) or not",MoreMountains.Tools.InformationAttribute.InformationType.Info,false)]
    31.         /// the initial delay during which the projectile can't be destroyed
    32.         public float InitialInvulnerabilityDuration=0f;
    33.         /// should the projectile damage its owner ?
    34.         public bool DamageOwner = false;
    35.        
    36.         protected Weapon _weapon;
    37.         protected GameObject _owner;
    38.         protected Vector3 _movement;
    39.         protected float _initialSpeed;
    40.         protected SpriteRenderer _spriteRenderer;
    41.         protected DamageOnTouch _damageOnTouch;
    42.         protected WaitForSeconds _initialInvulnerabilityDurationWFS;
    43.         protected Collider _collider;
    44.         protected Collider2D _collider2D;
    45.         protected Rigidbody _rigidBody;
    46.         protected Rigidbody2D _rigidBody2D;
    47.         protected bool _facingRightInitially;
    48.         protected bool _initialFlipX;
    49.         protected Vector3 _initialLocalScale;
    50.         protected bool _shouldMove = true;
    51.         protected Health _health;
    52.  
    53.         /// <summary>
    54.         /// On awake, we store the initial speed of the object
    55.         /// </summary>
    56.         protected virtual void Awake ()
    57.         {
    58.             _facingRightInitially = ProjectileIsFacingRight;
    59.             _initialSpeed = Speed;
    60.             _health = GetComponent<Health> ();
    61.             _collider = GetComponent<Collider> ();
    62.             _collider2D = GetComponent<Collider2D>();
    63.             _spriteRenderer = GetComponent<SpriteRenderer> ();
    64.             _damageOnTouch = GetComponent<DamageOnTouch>();
    65.             _rigidBody = GetComponent<Rigidbody> ();
    66.             _rigidBody2D = GetComponent<Rigidbody2D> ();
    67.             _initialInvulnerabilityDurationWFS = new WaitForSeconds (InitialInvulnerabilityDuration);
    68.             if (_spriteRenderer != null) {    _initialFlipX = _spriteRenderer.flipX ;        }
    69.             _initialLocalScale = transform.localScale;
    70.         }
    71.  
    72.         /// <summary>
    73.         /// Handles the projectile's initial invincibility
    74.         /// </summary>
    75.         /// <returns>The invulnerability.</returns>
    76.         protected virtual IEnumerator InitialInvulnerability()
    77.         {
    78.             if (_damageOnTouch == null) { yield break; }
    79.             if (_weapon == null) { yield break; }
    80.  
    81.             _damageOnTouch.ClearIgnoreList();
    82.             _damageOnTouch.IgnoreGameObject(_weapon.Owner.gameObject);
    83.             yield return _initialInvulnerabilityDurationWFS;
    84.             if (DamageOwner)
    85.             {
    86.                 _damageOnTouch.StopIgnoringObject(_weapon.Owner.gameObject);
    87.             }
    88.         }
    89.  
    90.         /// <summary>
    91.         /// Initializes the projectile
    92.         /// </summary>
    93.         protected virtual void Initialization()
    94.         {
    95.             Speed = _initialSpeed;
    96.             ProjectileIsFacingRight = _facingRightInitially;
    97.             if (_spriteRenderer != null) {    _spriteRenderer.flipX = _initialFlipX;    }
    98.             transform.localScale = _initialLocalScale;  
    99.             _shouldMove = true;
    100.             _damageOnTouch?.InitializeFeedbacks();
    101.  
    102.  
    103.             if (_collider != null)
    104.             {
    105.                 _collider.enabled = true;
    106.             }
    107.             if (_collider2D != null)
    108.             {
    109.                 _collider2D.enabled = true;
    110.             }
    111.         }
    112.  
    113.         /// <summary>
    114.         /// On update(), we move the object based on the level's speed and the object's speed, and apply acceleration
    115.         /// </summary>
    116.         protected virtual void FixedUpdate ()
    117.         {
    118.             base.Update ();
    119.             if (_shouldMove)
    120.             {
    121.                 Movement();
    122.             }
    123.         }
    124.  
    125.         /// <summary>
    126.         /// Handles the projectile's movement, every frame
    127.         /// </summary>
    128.         public virtual void Movement()
    129.         {
    130.             _movement = Direction * (Speed / 10) * Time.deltaTime;
    131.             //transform.Translate(_movement,Space.World);
    132.             if (_rigidBody != null)
    133.             {
    134.                 _rigidBody.MovePosition (this.transform.position + _movement);
    135.             }
    136.             if (_rigidBody2D != null)
    137.             {
    138.                 _rigidBody2D.MovePosition(this.transform.position + _movement);
    139.             }
    140.             // We apply the acceleration to increase the speed
    141.             Speed += Acceleration * Time.deltaTime;
    142.         }
    143.  
    144.         /// <summary>
    145.         /// Sets the projectile's direction.
    146.         /// </summary>
    147.         /// <param name="newDirection">New direction.</param>
    148.         /// <param name="newRotation">New rotation.</param>
    149.         /// <param name="spawnerIsFacingRight">If set to <c>true</c> spawner is facing right.</param>
    150.         public virtual void SetDirection(Vector3 newDirection, Quaternion newRotation, bool spawnerIsFacingRight=true)
    151.         {
    152.             if (DirectionCanBeChangedBySpawner)
    153.             {
    154.                 Direction = newDirection;
    155.             }
    156.             if (ProjectileIsFacingRight != spawnerIsFacingRight)
    157.             {
    158.                 Flip ();
    159.             }
    160.             if (FaceDirection)
    161.             {
    162.                 transform.rotation = newRotation;
    163.             }
    164.         }
    165.  
    166.         /// <summary>
    167.         /// Flip the projectile
    168.         /// </summary>
    169.         protected virtual void Flip()
    170.         {
    171.             if (_spriteRenderer != null)
    172.             {
    173.                 _spriteRenderer.flipX = !_spriteRenderer.flipX;
    174.             }  
    175.             else
    176.             {
    177.                 this.transform.localScale = Vector3.Scale(this.transform.localScale,FlipValue) ;
    178.             }
    179.         }
    180.  
    181.         /// <summary>
    182.         /// Sets the projectile's parent weapon.
    183.         /// </summary>
    184.         /// <param name="newWeapon">New weapon.</param>
    185.         public virtual void SetWeapon(Weapon newWeapon)
    186.         {
    187.             _weapon = newWeapon;
    188.         }
    189.  
    190.         /// <summary>
    191.         /// Sets the projectile's owner.
    192.         /// </summary>
    193.         /// <param name="newOwner">New owner.</param>
    194.         public virtual void SetOwner(GameObject newOwner)
    195.         {
    196.             _owner = newOwner;
    197.             DamageOnTouch damageOnTouch = this.gameObject.MMGetComponentNoAlloc<DamageOnTouch>();
    198.             if (damageOnTouch != null)
    199.             {
    200.                 damageOnTouch.Owner = newOwner;
    201.                 damageOnTouch.Owner = newOwner;
    202.                 if (!DamageOwner)
    203.                 {
    204.                     damageOnTouch.IgnoreGameObject(newOwner);
    205.                 }
    206.             }
    207.         }
    208.  
    209.         /// <summary>
    210.         /// On death, disables colliders and prevents movement
    211.         /// </summary>
    212.         public virtual void StopAt()
    213.         {
    214.             if (_collider != null)
    215.             {
    216.                 _collider.enabled = false;
    217.             }
    218.             if (_collider2D != null)
    219.             {
    220.                 _collider2D.enabled = false;
    221.             }
    222.            
    223.             _shouldMove = false;
    224.         }
    225.  
    226.         /// <summary>
    227.         /// On death, we stop our projectile
    228.         /// </summary>
    229.         protected virtual void OnDeath()
    230.         {
    231.             StopAt ();
    232.         }
    233.  
    234.         /// <summary>
    235.         /// On enable, we trigger a short invulnerability
    236.         /// </summary>
    237.         protected override void OnEnable ()
    238.         {
    239.             base.OnEnable ();
    240.  
    241.             Initialization();
    242.             if (InitialInvulnerabilityDuration>0)
    243.             {
    244.                 StartCoroutine(InitialInvulnerability());
    245.             }
    246.  
    247.             if (_health != null)
    248.             {
    249.                 _health.OnDeath += OnDeath;
    250.             }
    251.         }
    252.  
    253.         /// <summary>
    254.         /// On disable, we plug our OnDeath method to the health component
    255.         /// </summary>
    256.         protected override void OnDisable()
    257.         {
    258.             base.OnDisable ();
    259.             if (_health != null)
    260.             {
    261.                 _health.OnDeath -= OnDeath;
    262.             }          
    263.         }
    264.     }  
    265. }
     
    Last edited: Feb 7, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
  3. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    thx.
     
  4. keepthachange

    keepthachange

    Joined:
    Oct 15, 2014
    Posts:
    87
    I got it, but I feel stupid it took me so long to do it lol.