Hi, I am creating a local multiplayer fight game with the new input system from 2020. Everything works fine, but when the character dies you can still move him and attack. Only the first player can’t move but still can attack. I don’t know why their still moving, the fact that the first player can’ move anymore makes me think I am on the right direction, but can’t figure out what is missing.
I saw a lot of methods how to fix this problem when i researched it, but honestly i really don’t understand how to adapt them to my script/s, so that it functions as it should.
i have three scripts: MovementInputHandler, Movement and the Damage_Script.
Here you can see them:
First the MovementInputHandler Script: using System.Linq; using UnityEngine; using UnityEngine.InputSystem;
public class MovementInputHandler : MonoBehaviour
{
private PlayerInput playerInput;
private Movement movementScript;
void Awake()
{
playerInput = gameObject.GetComponent<PlayerInput>();
var movementScripts = FindObjectsOfType<Movement>();
var newIndex = playerInput.playerIndex;
movementScript = movementScripts.FirstOrDefault(move => move.playerIndex == newIndex);
}
public void OnMove(InputAction.CallbackContext ctx)
{
if (movementScript != null)
{
movementScript.move = ctx.ReadValue<Vector2>();
}
}
public void OnAttack(InputAction.CallbackContext ctx)
{
if (ctx.ReadValue<float>() != 0 && movementScript != null)
{
movementScript.Attack();
}
}
}
This is the Movement Script: using UnityEngine;
public class Movement : MonoBehaviour
{
public int playerIndex = 0;
[HideInInspector]public Vector2 move;
float moveSpeed = 25f;
private bool facingLeft = false;
private Animator anim;
private Rigidbody2D rb;
private BoxCollider2D boxCollider2d;
[SerializeField] private Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 10;
void Awake()
{
boxCollider2d = transform.GetComponent<BoxCollider2D>();
}
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
public void Attack()
{
anim.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<Damage_Script>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
void FixedUpdate()
{
rb.velocity = new Vector2(move.x * moveSpeed, rb.velocity.y);
anim.SetFloat("speed", Mathf.Abs(rb.velocity.x));
if (move.x < 0 && !facingLeft)
reverseImage();
else if (move.x > 0 && facingLeft)
reverseImage();
}
void reverseImage()
{
facingLeft = !facingLeft;
Vector2 theScale = rb.transform.localScale;
theScale.x *= -1;
rb.transform.localScale = theScale;
}
}
And finally the Damage Script:
using UnityEngine;
public class Damage_Script : MonoBehaviour
{
public Animator anim;
private int currentHealth;
public HealthBar healthBar;
public int maxHealth = 100;
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
anim.SetTrigger("Hurt");
healthBar.SetHealth(currentHealth);
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
GameObject.FindObjectOfType<Movement>().enabled = false;
}
}
Hope you can help…