using System.Collections; using System.Collections.Generic; using UnityEngine;…
- User
using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(LineRenderer))] public class Grappler : MonoBehaviour, IGadget { public enum eMode { gIdle, gOut, gRetract, gPull} [Header("Inscribed")] [Tooltip("Speed at which Grappler extends (doubled in gRetract mode)")] public float grappleSpd = 10; [Tooltip("Maximum length that Grappler will reach")] public float maxLength = 7.25f; [Tooltip("Minimum distance of Grappler from Dray")] public float minLength = 0.5f; [Tooltip("Health deducted when Dray ends a grapple on an unsafe tile")] public int unsafeTileHealthPenalty = 2;
[Header("Dynamic")] [SerializeField] private eMode _mode = eMode.gIdle; public eMode mode { get { return _mode; } private set { _mode = value; } } private LineRenderer line; private Rigidbody2D rigid; private Collider2D colld; private Vector3 p0, p1; private int facing; private Dray dray; private System.Func<IGadget, bool> gadgetDoneCallback; private Vector2[] directions = new Vector2[] { Vector2.right, Vector2.up, Vector2.left, Vector2.down }; private Vector3[] dirV3s = new Vector3[] { Vector3.right, Vector3.up, Vector3.left, Vector3.down }; private void Awake() { //Get component references to use throughout the script line = GetComponent<LineRenderer>(); rigid = GetComponent<Rigidbody2D>(); colld = GetComponent<Collider2D>(); } void Start() { gameObject.SetActive(false); //Initially disable this GameObject } void SetGrappleMode(eMode newMode) { switch (newMode) { case eMode.gIdle: transform.DetachChildren(); //Release any child Transforms gameObject.SetActive(false); if (dray != null && dray.controlledBy == this as IGadget) { dray.controlledBy = null; dray.physicsEnabled = true; } break; case eMode.gOut: gameObject.SetActive(true); rigid.velocity = directions[facing] * grappleSpd; break; case eMode.gRetract: rigid.velocity = -directions[facing] * (grappleSpd * 2); break; case eMode.gPull: p1 = transform.position; rigid.velocity = Vector2.zero; dray.controlledBy = this; dray.physicsEnabled = false; break; } mode = newMode; } private void FixedUpdate() { p1 = transform.position; line.SetPosition(1, p1); switch (mode) { case eMode.gOut: //Grappler shooting out //See if the Grappler reached its limit without hitting anything if ((p1 - p0).magnitude >= maxLength) { SetGrappleMode(eMode.gRetract); } break; case eMode.gRetract: //Grappler missed; return at double speed //Check to see if the Grappler is no longer in front of Dray if (Vector3.Dot((p1 - p0), dirV3s[facing]) < 0) GrappleDone(); break; case eMode.gPull: if ((p1-p0).magnitude > minLength) { //Move Dray toward the Grappler hit point p0 += dirV3s[facing] * grappleSpd * Time.fixedDeltaTime; dray.transform.position = p0; line.SetPosition(0, p0); //Stop Grappler from moving with Dray transform.position = p1; } else { //Dray is close enough to stop grappling p0 = p1 - (dirV3s[facing] * minLength); dray.transform.position = p0; //Check whether Dray landed on an unsafe tile Vector2 checkPos = (Vector2)p0 + new Vector2(0, -0.25f); if (MapInfo.UNSAFE_TILE_AT_VECTOR2(checkPos)) { //Dray landed on an unsafe tile dray.ResetInRoom(unsafeTileHealthPenalty); } GrappleDone(); } break; } } //Ensures that p1 is aligned with the Grappler head private void LateUpdate() { p1 = transform.position; line.SetPosition(1, p1); } ///<summary> ///Called when the Grappler hits a Trigger or Collider in the GrapTiles, Items, or Enemies Physics Layers (Grappler's Collider is a Trigger) /// </summary> /// <param name="coll"></param> void OnTriggerEnter2D(Collider2D colld) { //The Grappler has collided with something, but what? string otherLayer = LayerMask.LayerToName(colld.gameObject.layer); switch (otherLayer) { case "Items": //We've possibly hit a PickUp PickUp pup = colld.GetComponent<PickUp>(); if (pup == null) return; //If this IS a PickUp, make it a child of this Transform so it moves with the Grappler head pup.transform.SetParent(transform); pup.transform.localPosition = Vector3.zero; SetGrappleMode(eMode.gRetract); break; case "Enemies": //We've hit an Enemy //The Grappler should return when it hits an Enemy Enemy e = colld.GetComponent<Enemy>(); if (e != null) { SetGrappleMode(eMode.gRetract); StunEnemy(); } //Damaging the Enemy is handled by the DamageEffect & Enemy scripts break; case "GrapTiles": //We've hit a GrapTile //SetGrappleMode(eMode.gRetract); SetGrappleMode(eMode.gPull); break; default: SetGrappleMode(eMode.gRetract); break; } } void StunEnemy() { } void GrappleDone() { SetGrappleMode(eMode.gIdle); //Callback to Dray so he returns to normal controls gadgetDoneCallback(this); } #region IGadget_Implementation //Implementation of IGadget //Called by Dray to use this IGadget public bool GadgetUse(Dray tDray, System.Func<IGadget,bool> tCallback) { if (mode != eMode.gIdle) return false; dray = tDray; gadgetDoneCallback = tCallback; transform.localPosition = Vector3.zero; facing = dray.GetFacing(); p0 = dray.transform.position; p1 = p0 + (dirV3s[facing] * minLength); gameObject.transform.position = p1; gameObject.transform.rotation = Quaternion.Euler(0, 0, 90 * facing); line.positionCount = 2; line.SetPosition(0, p0); line.SetPosition(1, p1); SetGrappleMode(eMode.gOut); return true; } //Called by Dray if he is hit while grappling and mode != eMode.inHit public bool GadgetCancel() { //If pulling Dray to a wall, ignore GadgetCancel if (mode == eMode.gPull) return false; SetGrappleMode(eMode.gIdle); gameObject.SetActive(false); return true; } //string name is already part of Grappler (inherited from Object) #endregion - Assistant
To stun the enemy for 1 second, you can use a Coroutine that waits for 1 second before unstunning the enemy. Here's an example of how you can implement the StunEnemy() method:
void StunEnemy() { // Get the Enemy component from the collided object Enemy enemy = colld.GetComponent<Enemy>(); // Check if the enemy component exists and is not already stunned if (enemy != null && !enemy.IsStunned()) { // Stun the enemy for 1 second enemy.Stun(1.0f); } }Assuming you have a
Stun(float duration)method in your Enemy script that sets the enemy's stun status and waits for the duration before unstunning, this would stun the enemy for 1 second. - User
- Assistant
In the Enemy script, add the
Stun(float duration)method that sets the enemy's stun status and waits for the duration before unstunning:using System.Collections; using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour, ISwappable { // ... private bool stunned = false; // ... public void Stun(float duration) { if (!stunned) { StartCoroutine(StunCoroutine(duration)); } } private IEnumerator StunCoroutine(float duration) { stunned = true; // TODO: Perform any actions or effects when the enemy is stunned yield return new WaitForSeconds(duration); // TODO: Perform any actions or effects when the stun duration is over stunned = false; } public bool IsStunned() { return stunned; } // ... }With this implementation, you can now call the
Stun(float duration)method on an Enemy instance to stun it for the specified duration. - User
the enemy is still not being stunned
- Assistant
Apologies for the confusion. It seems like the StunEnemy() method in the Grappler script is not calling the correct Stun() method or checking if the enemy is already stunned before stunning it again.
To fix this issue, you can update the StunEnemy() method in the Grappler script to directly call the Stun() method on the Enemy component of the collided object and check if the enemy is already stunned. Here's an updated version of the StunEnemy() method:
void StunEnemy() { // Get the Enemy component from the collided object Enemy enemy = colld.GetComponent<Enemy>(); // Check if the enemy component exists and is not already stunned if (enemy != null && !enemy.IsStunned()) { // Stun the enemy for 1 second enemy.Stun(1.0f); } }Make sure that the Enemy class has the
IsStunned()method implemented as shown in the previous responses. This should correctly stun the enemy for 1 second when the Grappler collides with it. - User
the enemies are still not being stunned
- Assistant
Apologies for the oversight. It seems like the StunEnemy() method in the Grappler script is not being called when the enemy is hit.
To ensure that the StunEnemy() method is executed, you need to update the OnTriggerEnter2D() method in the Grappler script. Currently, it only checks if the collided object has an Enemy component but does not call the StunEnemy() method.
Here's the updated OnTriggerEnter2D() method in the Grappler script:
void OnTriggerEnter2D(Collider2D colld) { //The Grappler has collided with something, but what? string otherLayer = LayerMask.LayerToName(colld.gameObject.layer); switch (otherLayer) { case "Items": //We've possibly hit a PickUp // ... (existing code) break; case "Enemies": //We've hit an Enemy // Get the Enemy component from the collided object Enemy enemy = colld.GetComponent<Enemy>(); // Check if the enemy component exists if (enemy != null) { // Stun the enemy StunEnemy(enemy); } break; case "GrapTiles": //We've hit a GrapTile // ... (existing code) break; default: // ... (existing code) break; } } void StunEnemy(Enemy enemy) { if (!enemy.IsStunned()) { enemy.Stun(1.0f); } } - User
still not working
- Assistant
Apologies for the confusion. It seems the issue lies with the calling of the StunEnemy() method in the Grappler script. The OnTriggerEnter2D() method is currently calling the default Unity method OnTriggerEnter2D(Collider2D colld) instead of the local StunEnemy() method.
To fix this issue, you can update the OnTriggerEnter2D() method in the Grappler script to call the StunEnemy() method as follows:
void OnTriggerEnter2D(Collider2D colld) { //The Grappler has collided with something, but what? string otherLayer = LayerMask.LayerToName(colld.gameObject.layer); switch (otherLayer) { case "Items": //We've possibly hit a PickUp // ... (existing code) break; case "Enemies": //We've hit an Enemy // Stun the enemy StunEnemy(colld); break; case "GrapTiles": //We've hit a GrapTile // ... (existing code) break; default: // ... (existing code) break; } }Additionally, update the StunEnemy() method to retrieve the Enemy component from the collider and call the stun method: