- Home /
Local Multiplayer Error in Unity2D
Hello, i am recently developing a 2D Platform Fighter, and i just implemented local multiplayer with Unity's new input system. Everything seems to work fine, until i tried to attack a player as player 2. The Problem can be seen in the link video below :
https://drive.google.com/file/d/1ybFKycCHhMfCmIMNeLiYDkroX7TcX0JU/view?usp=sharing
As you can see at 00:16, i tried to move Player 2, and it actually worked fine, until i tried to attack the other player, i expected the player would got knockbacked to the opposite position, but it turned out the player just moved the only Y position. But when i used the Player 1 to attack Player 2, the knockback just work fine. Can anyone know how to resolve this different behavior among players? They share the same code and components, and even they are exactly the same game object, i just copied one of them so technically they should be the same.
Here's my knockback code :
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TakingDamage : MonoBehaviour { int startPercentage = 0; public int percentage;
Rigidbody2D rigid;
Animator animator;
PlayerInputControl player;
//Knockback
float knockback;
float knockbackLength;
float knockbackCount = 0f;
bool knockbacked;
// Start is called before the first frame update
void Start()
{
percentage = startPercentage;
rigid = GetComponent<Rigidbody2D>();
player = GetComponent<PlayerInputControl>();
animator = GetComponent<Animator>();
}
private void FixedUpdate()
{
if (knockbacked)
{
animator.SetBool("isKnockbacked", true);
if (knockbackCount > 0)
{
player.canNormalJump = false;
player.canMove = false;
knockbackCount -= Time.fixedDeltaTime;
}
if (knockbackCount <= 0)
{
knockbacked = false;
this.transform.GetChild(0).gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
this.transform.GetChild(0).gameObject.transform.localScale = new Vector3(1, 1, 1);
rigid.velocity = new Vector2(0, rigid.velocity.y);
}
}
if (!knockbacked)
{
animator.SetBool("isKnockbacked", false);
player.canMove = true;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "hitbox")
{
Vector2 direction = (collision.gameObject.transform.parent.parent.position - this.transform.position).normalized;
percentage += collision.GetComponent<PlayerAttackHitbox>().damage;
knockback = percentage * 0.4f;
if (collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign)
{
knockbackCount = knockback * 0.05f;
knockbacked = true;
if (direction.x < 0)
{
rigid.velocity = new Vector2(direction.x + knockback, knockback);
}
else if(direction.x >= 0){
rigid.velocity = new Vector2(-(direction.x + knockback), knockback);
}
Debug.Log(rigid.velocity);
}
else if(collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign && collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().isLightAttacking)
{
knockbackCount = knockback * 0.03f;
knockbacked = true;
if (direction.x < 0)
{
rigid.velocity = new Vector2((direction.x + knockback) * 0.5f, knockback / 3);
}
else if (direction.x >= 0)
{
rigid.velocity = new Vector2(-(direction.x + knockback) * 0.5f, knockback / 3);
}
Debug.Log(rigid.velocity);
}
//Debug.Log(direction.x + knockback + "," + knockback);
//StartCoroutine(Knockback(percentage * 0.1f, percentage * 0.01f, collision.gameObject.transform));
else if (!collision.transform.parent.parent.gameObject.GetComponent<PlayerInputControl>().knockbackSign)
{
animator.SetTrigger("Hurt");
}
}
}
Your answer
Follow this Question
Related Questions
Multiplayer Rigidbody Vibration 2 Answers
If my object moves -- Detect on carachter controller? 0 Answers
Rigidbody.AddForce depending on frame rate? 1 Answer
Network Transform Slow Rotations 0 Answers