SwordScript.cs (13348B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class SwordScript : MonoBehaviour { 6 7 private bool MouseInput; //Are you doing mouse movement or key input for keyboard 8 public bool swordDone = true; //Sword finished thrust 9 public bool isBlocked = false; //Was blocked 10 public bool isFalling; 11 12 public float swordSpeed; //How fast the sword moves under keyboard input 13 public float swordMouseSpeed; //How fast the sword moves under mouse input 14 15 public float thrustSpeed; //How fast the sword is thrusted forward 16 public float thrustTime; //How long the thrust lasts until it returns to original spot 17 18 [Range(0, 5)] 19 public float Radius = 1f; //How far the sword is away from player in normal position 20 [Range(0, 10)] 21 public float maxRadius; //How far the sword is allowed to go during thrust 22 23 public float dangerTime = 1f; //How long the sword can be in the danger zone till the enemy dies 24 25 public float basicHitDistance; //How far the enemy will go on a basic hit 26 public float thrustHitDistance; //How far the enemy will go on a thrust hit 27 public float blockHitDistanceTPunish; //How far the enemy will go on blocked hit on thrust 28 public float blockHitDistanceTDefensive; //How far player blocking will be pushed on thrust 29 public float noTHitDistancePunish; //normal hit push distance 30 public float noTHitDistanceDefensive; 31 32 public Vector3 blockedOffset; 33 34 public GameObject Owner; //Who owns the sword 35 private GameObject enemy; 36 37 public Coroutine thrust; 38 public Coroutine killMovement; 39 40 private Coroutine count; //The coroutine timer for the danger zone 41 42 private Animator SlimeAnim; //animator of owner slime of sword 43 private Rigidbody rb; //Rigid body of sword 44 45 private float theY = 1; //The static height of the sword 46 private float angle; //angle for sword when using keyboard 47 private float h; 48 private float v; 49 50 private Vector3 center; //Basically location of player 51 private Vector3 offset; //changing sword location based on rotation and keeping radius from player 52 53 private bool lastState; //handeling thrust button pushes/button pushes in general 54 private bool swordThrusted = false; //Started thrust 55 private bool inZone = false; //In the zone or not 56 //private bool hasBeenBlocked = false; 57 58 private PlayerMovement playerMovement; //Player movement script holder 59 private BlockScript blockScript; //Block Hitbox Script 60 private HitPercentageHolder hitPercentageHolder; 61 62 private string inputHoriSw; 63 private string inputVerSw; 64 private string inputThru; 65 66 private Vector3 swordTarget; 67 private Vector3 pointPosition; 68 69 [HideInInspector] 70 public bool thrustHit; 71 void Start () 72 { 73 rb = GetComponent<Rigidbody>(); 74 SlimeAnim = Owner.transform.GetChild(0).GetComponent<Animator>(); 75 76 playerMovement = Owner.GetComponent<PlayerMovement>(); 77 blockScript = GetComponentInChildren<BlockScript>(); 78 hitPercentageHolder = GameObject.Find("PercentageHits").GetComponent<HitPercentageHolder>(); 79 80 transform.localPosition = (Owner.transform.position * Radius); 81 82 inputHoriSw = playerMovement.inputHoriSw; 83 inputVerSw = playerMovement.inputVerSw; 84 inputThru = playerMovement.inputThru; 85 MouseInput = playerMovement.MouseInput; 86 87 Physics.IgnoreCollision(Owner.GetComponent<Collider>(), GetComponent<Collider>()); 88 89 } 90 91 // Update is called once per frame 92 void Update () { 93 center = Owner.transform.position; //center variable is set to player location 94 95 //Dont let sword go past max radius during thrust 96 if (!swordDone && !isBlocked) 97 { 98 if (Vector3.Distance(transform.position, Owner.transform.position) > maxRadius) 99 { 100 rb.velocity = Vector3.zero; 101 } 102 } 103 104 } 105 private void FixedUpdate() 106 { 107 transform.LookAt(2 * transform.position - new Vector3(center.x, transform.position.y, center.z)); //Set rotation of sword to look away from player 108 109 if (swordDone && !playerMovement.dashProcess && !isBlocked) //If the sword is not being thrusted, and player is not in dash take input for moving sword 110 { 111 MoveSword(); 112 } 113 if (isBlocked) { 114 Blocked(); 115 } 116 117 //Treat a GetAxis like GetButtonDown 118 swordThrusted = LikeOnKeyDown(inputThru); 119 //Check when thust is pressed 120 if (Input.GetAxis(inputThru) != 0) //Checks if the fire button is pressed and the sword is not already thrusted 121 { 122 //When key is up and not dashing or thrusting start a thrust 123 if (swordThrusted && swordDone && !playerMovement.dashProcess) 124 { 125 ThrustSword(); 126 } 127 } 128 } 129 130 public void MoveSword() { 131 //Key input 132 if (!MouseInput) { 133 //Get horizontal sword movement (Like movement but just horizontal) 134 h = Input.GetAxisRaw(inputHoriSw); 135 v = Input.GetAxisRaw(inputVerSw); 136 137 //swordPoint.transform.position = center; 138 if (Mathf.Abs(h) > 0.05 || Mathf.Abs(v) > 0.05) { 139 float pointX = center.x + Radius * h; 140 float pointZ = center.z + Radius * -v; 141 pointPosition = new Vector3(pointX, theY, pointZ); 142 } 143 offset = transform.position - center; 144 Vector3 swordTarget = Vector3.RotateTowards(offset, (pointPosition - center), swordMouseSpeed, 0f); 145 swordTarget.y = center.y + theY; 146 transform.position = center + swordTarget.normalized * Radius; 147 148 } //mouse input 149 else { 150 //Generate a plane horizontally from the player position to get raycast hits 151 Plane playerPlane = new Plane(Vector3.up, center); 152 if (Camera.main != null) 153 { 154 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //ray from camera angle diraction from mouse position 155 float hitdist = 0.0f; //hit distance 156 //when ray hits generated plane 157 if (playerPlane.Raycast(ray, out hitdist)) { 158 //location where raycast hit 159 Vector3 targetPoint = ray.GetPoint(hitdist); 160 //Debug.Log(targetPoint); 161 //set offset (basically radius) 162 offset = transform.position - center; 163 //set the height to static variable 164 //Rotate sword towards ray hit position based on speed 165 Vector3 swordTarget = Vector3.RotateTowards(offset, (targetPoint - center), swordMouseSpeed, 0f); 166 swordTarget.y = center.y + theY; 167 //Set position to that rotated towards 168 transform.position = center + swordTarget.normalized * Radius; 169 170 } 171 } 172 } 173 //Keep static height 174 transform.position = new Vector3(transform.position.x, (theY + center.y), transform.position.z); 175 } 176 177 private bool LikeOnKeyDown(string axis) 178 { 179 //Converts axis input to just true/false like button press 180 var currentState = Input.GetAxis(axis) > 0.1; 181 if (currentState && lastState) 182 { 183 return false; 184 } 185 186 lastState = currentState; 187 return currentState; 188 } 189 190 private void ThrustSword() 191 { 192 //Started thrust sword is not done thrust 193 swordDone = false; 194 195 if (AudioManager.getInstance() != null) 196 { 197 AudioManager.getInstance().Find("thrust").source.Play(); 198 } 199 200 //play thrust animation 201 SlimeAnim.Play("THRUST"); 202 203 //player looks where he is thrusting 204 Owner.transform.LookAt(new Vector3(transform.position.x, Owner.transform.position.y, transform.position.z)); 205 206 //change velocity of sword to set speed forwards 207 rb.velocity += transform.forward * thrustSpeed; 208 209 //Start the trust debuff handler in PlayerMovement script 210 playerMovement.thrustDebuff = true; 211 //half speed of player 212 playerMovement.currentSpeed = playerMovement.currentSpeed / 2; 213 //Start the heal debuff timer in PlayerMovement script 214 playerMovement.killMovement = playerMovement.StartCoroutine(playerMovement.KillMovement(playerMovement.thrustDebuffTime)); 215 //Stop the thrust (return to hand and set velocity to zero after set time) 216 thrust = StartCoroutine(StopThrust(thrustTime)); 217 218 if (AudioManager.getInstance() != null && AudioManager.getInstance().Find("thrust").source.mute) 219 { 220 AudioManager.getInstance().Find("thrust").source.Stop(); 221 AudioManager.getInstance().Find("thrust").source.mute = false; 222 } 223 } 224 225 private IEnumerator StopThrust(float waitTime) 226 { 227 yield return new WaitForSeconds(waitTime); 228 swordDone = true; 229 rb.velocity = Vector3.zero; 230 //Can move sword again set to position incase mouse position was changed during thrust 231 MoveSword(); 232 } 233 234 private void OnTriggerEnter(Collider other) 235 { 236 if (other.transform.parent != Owner.transform && other.gameObject.name == "DefendArea" && swordDone && !inZone) //Checks if the zone is it's own and if it's not in the zone yet 237 { 238 enemy = other.transform.parent.gameObject; 239 PlayerMovement enemyPM = enemy.GetComponent<PlayerMovement>(); 240 enemyPM.SlimeAnim.Play("TAKEDAMAGE2"); 241 Debug.Log(Owner.name + " !Hit! " + enemy.name); 242 count = StartCoroutine(Countdown(dangerTime)); //Begins the death countdown 243 } 244 if (other.transform.parent != transform && other.gameObject.GetComponent("BlockScript") != null && !isBlocked) //Checks if the collision is the parent object 245 { 246 //Starts the block process 247 isBlocked = true; 248 blockScript.blockSword(other.transform.parent.gameObject); 249 SwordScript enemySS = other.transform.parent.gameObject.GetComponent<SwordScript>(); 250 enemy = enemySS.Owner; 251 Debug.Log(Owner.name + " !Blocked! " + enemy.name); 252 PlayerMovement enemyPM = enemy.GetComponent<PlayerMovement>(); 253 playerMovement.blockCenter = (Owner.transform.position + enemy.transform.position) / 2; 254 enemyPM.blockCenter = (Owner.transform.position + enemy.transform.position) / 2; 255 if (thrustHit) 256 { 257 if (AudioManager.getInstance() != null) 258 { 259 AudioManager.getInstance().Find("blockthrust").source.Play(); 260 } 261 262 Debug.Log( Owner.name + " PUNISHED"); 263 enemyPM.startHitSequence(blockHitDistanceTDefensive, hitPercentageHolder.normalBlockPercentage, 0); 264 playerMovement.startHitSequence(blockHitDistanceTPunish, hitPercentageHolder.punishBlockPercentage); 265 266 thrustHit = false; 267 } 268 else 269 { 270 if (AudioManager.getInstance() != null) 271 { 272 AudioManager.getInstance().Find("swordonsword").source.Play(); 273 } 274 275 Debug.Log(Owner.name + " normal hit."); 276 enemyPM.startHitSequence(noTHitDistanceDefensive, 0, 0); 277 //playerMovement.startHitSequence(noTHitDistancePunish); 278 } 279 280 } 281 if (other.transform.parent != Owner.transform && other.gameObject.name == "DefendArea" && !swordDone) //Checks if the zone is it's own and if it's not in the zone yet 282 { 283 if (AudioManager.getInstance() != null) 284 { 285 AudioManager.getInstance().Find("hurt").source.Play(); 286 } 287 288 enemy = other.transform.parent.gameObject; 289 Debug.Log(Owner.name + " !Dead! " + enemy.name); 290 PlayerMovement enemyPM = enemy.GetComponent<PlayerMovement>(); 291 enemyPM.SlimeAnim.Play("TAKEDAMAGE"); 292 enemyPM.blockCenter = (enemy.transform.position + Owner.transform.position) / 2; 293 enemyPM.startHitSequence(thrustHitDistance, hitPercentageHolder.thrustBodyHitPercentage); 294 } 295 } 296 297 private void OnTriggerExit(Collider other) 298 { 299 if (other.transform.parent != Owner.transform && other.gameObject.name == "DefendArea" && inZone) //Checks to see if the sword has exited the zone 300 { 301 StopCoroutine(count); //Resets and stops the death countdown 302 inZone = false; //Exits the zone 303 } 304 } 305 306 private IEnumerator Countdown(float waitTime) 307 { 308 inZone = true; //Enters the zone 309 yield return new WaitForSeconds(waitTime); 310 PlayerMovement enemyPM = enemy.GetComponent<PlayerMovement>(); 311 enemyPM.blockCenter = (enemy.transform.position + Owner.transform.position) / 2; 312 enemyPM.startHitSequence(basicHitDistance, hitPercentageHolder.normalBodyHitPercentage); 313 inZone = false; //Exits the zone 314 } 315 316 public void stopCoroutines() 317 { 318 StopCoroutine(thrust); 319 } 320 321 private void Blocked() 322 { 323 transform.position = Owner.transform.position + blockedOffset; 324 } 325 }