Search Unity

script problem "Spin" (Attack)

Discussion in 'Scripting' started by Deleted User, Jan 27, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hello everyone I have a problem the game I'm doing is a 3d Platform
    basically I have this script but I want to fix it even trying to change it does not change anything in short, I would like that when it happens "spin" (attack) will sweep away the enemy I hope you can help me with any problem :D (sorry but I'm not so experienced with script) here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //[RequireComponent(typeof(Player3DPlatformController))]
    5. [RequireComponent(typeof(AudioSource))]
    6. public class Crash : MonoBehaviour, IHittable
    7. {
    8. public LayerMask HittableItemsLayer;
    9. public CheckBoxColliderCollision spinCheck;
    10. public CheckBoxColliderCollision headCheck;
    11. public ParticleSystem spinParticleSystem;
    12. public Transform spinEffect;
    13. public Player3DPlatformController playerController;
    14. public float invencibityTime = 2f;
    15. public float blinkRate = 8f;
    16. public Renderer meshRender;
    17. private Animator anim;
    18. private readonly int spinState = Animator.StringToHash("Base Layer.SpinTornado");
    19. private bool spinning;
    20. private static Crash Instance;
    21. private bool invencible;
    22. private bool redColor;
    23. private float currentInvencibityTime = 0f;
    24. public DeathCamera deathCamera;
    25.  
    26. public static Vector3 Position{ get { return Instance.transform.position; } }
    27.  
    28. void Start()
    29. {
    30. anim = GetComponent<Animator>();
    31. //playerController = GetComponent<Player3DPlatformController>();
    32. spinParticleSystem.Stop();
    33. Instance = this;
    34. }
    35.  
    36. void Update()
    37. {
    38. spinning = false;
    39.  
    40. //fix the animation xz axis movement problem
    41. transform.position = playerController.transform.position;
    42. transform.rotation = playerController.transform.rotation;
    43.  
    44.  
    45. if (Input.GetButtonDown("ShowHUD"))
    46. HUDProvider.Instance.Show();
    47.  
    48. if (Input.GetButtonDown("Attack") &&
    49. anim.GetCurrentAnimatorStateInfo(0).nameHash != spinState)
    50. {
    51. anim.SetTrigger("spin");
    52. }
    53.  
    54.  
    55. if (anim.GetCurrentAnimatorStateInfo(0).nameHash == spinState)
    56. {
    57. if ((spinCheck.CollisionLayer.value & HittableItemsLayer) > 0 && spinCheck.HitTransform != null)
    58. {
    59. IHittable hitScript = (IHittable)spinCheck.HitTransform.GetComponent(typeof(IHittable));
    60. if (hitScript != null)
    61. {
    62. hitScript.OnHit(transform.position);
    63. }
    64. }
    65. spinning = true;
    66. }
    67.  
    68. anim.SetFloat("hspeed", playerController.input.sqrMagnitude);
    69. anim.SetFloat("vspeed", playerController.vspeed);
    70. anim.SetBool("ground", playerController.grounded);
    71. anim.SetBool("spinning", spinning);
    72. }
    73.  
    74. void FixedUpdate()
    75. {
    76. //bottom jump
    77. if (playerController.bottomCollision.HitTransform != null &&
    78. playerController.bottomCollision.IsColliding(HittableItemsLayer) &&
    79. playerController.bottomCollision.transform.position.y >
    80. playerController.bottomCollision.HitTransform.position.y +
    81. playerController.bottomCollision.HitTransform.localScale.y / 3f)
    82. {
    83. Jumpable jumpScript = playerController.bottomCollision.HitTransform.GetComponent<Jumpable>();
    84. if (jumpScript != null && playerController.vspeed < 0f)
    85. {
    86. jumpScript.OnJump();
    87. playerController.vspeed = jumpScript.jumpAcc;
    88. }
    89. }
    90.  
    91. //top collision
    92. if (headCheck.IsColliding(HittableItemsLayer) && headCheck.HitTransform != null)
    93. {
    94. Jumpable jumpScript = headCheck.HitTransform.GetComponent<Jumpable>();
    95. if (jumpScript != null && playerController.vspeed >= 0f)
    96. {
    97. jumpScript.OnJump();
    98. playerController.vspeed = -0.1f;
    99. }
    100. }
    101. }
    102.  
    103. public void StartSpinTornato()
    104. {
    105. audio.PlayOneShot(AudioProvider.Instance.GetRegisteredAudioClip("spin"));
    106. spinParticleSystem.Play();
    107. spinEffect.gameObject.SetActive(true);
    108. PrefabsProvider.Instance.InstanciateParticleSystem("Smoke", transform.position + transform.right);
    109. }
    110.  
    111. public void FinishSpinTornato()
    112. {
    113. anim.SetTrigger("finishSpin");
    114.  
    115. spinParticleSystem.Stop();
    116. spinEffect.gameObject.SetActive(false);
    117. PrefabsProvider.Instance.InstanciateParticleSystem("Smoke", transform.position + transform.right);
    118. }
    119.  
    120. public void LandingSmoke()
    121. {
    122. PrefabsProvider.Instance.InstanciateParticleSystem("Smoke", transform.position + Vector3.up);
    123. }
    124.  
    125. public virtual void OnHit(Vector3 position)
    126. {
    127. if (invencible)
    128. return;
    129.  
    130.  
    131.  
    132. if (HUDProvider.Instance.AkuAkus == 0)
    133. {
    134. KillCrash();
    135. return;
    136. } else
    137. HUDProvider.Instance.RemoveAkuAku();
    138.  
    139. audio.PlayOneShot(AudioProvider.Instance.GetRegisteredAudioClip("hit"));
    140.  
    141. playerController.vspeed = playerController.jumpAcc;
    142.  
    143. Vector3 direction = position - transform.position;
    144. direction.y = 0f;
    145. direction.z *= -1;
    146. direction.x *= -1;
    147.  
    148. playerController.transform.position += direction * 1.2f;
    149. invencible = true;
    150. StartCoroutine("InvencibityOffCoroutine", invencibityTime);
    151. StartCoroutine("ChangeMaterialColorCoroutine");
    152. }
    153.  
    154. void KillCrash()
    155. {
    156. anim.SetTrigger("kill");
    157. invencible = true;
    158. playerController.canControl = false;
    159. deathCamera.StartCameraDeath();
    160. enabled = false;
    161. audio.PlayOneShot(AudioProvider.Instance.GetRegisteredAudioClip("woah"));
    162.  
    163. }
    164.  
    165. IEnumerator InvencibityOffCoroutine(float time)
    166. {
    167. yield return new WaitForSeconds(time);
    168. invencible = false;
    169. }
    170.  
    171. IEnumerator ChangeMaterialColorCoroutine()
    172. {
    173. if (!redColor)
    174. meshRender.material.color = Color.red;
    175. else
    176. meshRender.material.color = Color.white;
    177.  
    178. redColor = !redColor;
    179.  
    180. float waitTime = 1f / blinkRate;
    181.  
    182. yield return new WaitForSeconds(waitTime);
    183. currentInvencibityTime += waitTime;
    184.  
    185. if (currentInvencibityTime >= 1f)
    186. {
    187. currentInvencibityTime = 0f;
    188. meshRender.material.color = Color.white;
    189. } else
    190. {
    191. StartCoroutine("ChangeMaterialColorCoroutine");
    192. }
    193. }
    194. }
    195.  

    Script Enemy:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(BoxCollider))]
    5. public abstract class Enemy : Jumpable, IHittable
    6. {
    7. private bool dying;
    8.  
    9. public override void OnJump()
    10. {
    11. if (dying)
    12. return;
    13.  
    14. AudioProvider.Instance.PlayAudioShotInPlayer("Enemy Dead");
    15. PrefabsProvider.Instance.InstanciateParticleSystem("EnemySmoke", transform.position, transform.lossyScale);
    16.  
    17. Destroy();
    18. }
    19.  
    20. public virtual void OnHit(Vector3 position)
    21. {
    22. if (rigidbody != null)
    23. return;
    24.  
    25. Vector3 direction = position - transform.position;
    26. direction.y = 0f;
    27. direction.z *= -1;
    28. direction.x *= -1;
    29.  
    30. AudioProvider.Instance.PlayAudioShotInPlayer("Enemy Hit");
    31.  
    32. gameObject.AddComponent<Rigidbody>();
    33. rigidbody.useGravity = false;
    34.  
    35. rigidbody.AddForce(direction * 40f, ForceMode.Impulse);
    36.  
    37. gameObject.AddComponent<ThrowItem>();
    38.  
    39. Destroy(gameObject, 3f);
    40. }
    41.  
    42. public virtual void Destroy()
    43. {
    44. dying = true;
    45. StartCoroutine("DeathCorroutine");
    46. Destroy(gameObject, 3f);
    47. }
    48.  
    49. void Start()
    50. {
    51. print("update");
    52. }
    53.  
    54. void Update()
    55. {
    56. transform.localScale -= Vector3.one * 0.08f;
    57. transform.Rotate(Vector3.up * 10f);
    58. transform.position += Vector3.up * -0.2f;
    59. }
    60.  
    61. IEnumerator DeathCorroutine()
    62. {
    63. yield return new WaitForSeconds(Time.deltaTime);
    64. Update();
    65. StartCoroutine("DeathCorroutine");
    66. }
    67. }



    Script Crab:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Crab : Enemy
    5. {
    6.  
    7. // Use this for initialization
    8. void Start()
    9. {
    10.  
    11. }
    12.  
    13. // Update is called once per frame
    14. void Update()
    15. {
    16.  
    17. }
    18. }
     
  2. Deleted User

    Deleted User

    Guest

  3. Deleted User

    Deleted User

    Guest