- Home /
Shooting myself in multiplayer 2D platformer?
Somehow the server player will never shoot itself but the client player will if it is moving forward. This is the PlayerMove script...
using UnityEngine;
using UnityEngine.Networking;
//Calls the player move script and has a network behavior function.
[RequireComponent(typeof(Rigidbody2D))]
public class PlayerMove : NetworkBehaviour
{
//Instantiates the bulletPrefab that was made in the files. Other objects can be called in here.
public GameObject bulletPrefab;
//This make a variable that stores the last change in x postion
public float lastXPositionChange = 0f;
public float acceleration = 1f;
//drag
public float deceleration = 0f;
//for movement
private Vector2 input;
//mainly for acceleration
private Rigidbody2D physics;
public override void OnStartLocalPlayer()
{
//Changes the color to red.
//WARNING! These changes only apply to the client side. The other player will not see these changes.
//These changes happen to everyone, but only on the client side for everyone.
GetComponent<MeshRenderer>().material.color = Color.red;
/*
//creates Rigidbody to alter gravity, drag and to eventually apply force for acceleration
physics = this.GetComponent<Rigidbody2D>();
physics.gravityScale = 1;
physics.drag = deceleration;
*/
}
//Debug.Log(this.myNetworkId.get());
void Update()
{
//Makes it so that you don't move everyone else's charactes when you type in your keys... LOL
if (!isLocalPlayer)
return;
//Moving forwards and backwards variables.
input.x = Input.GetAxis("Horizontal")*0.1f;
//Transforms, so it checks the keys.
transform.Translate(input.x, 0, 0);
//This gets the last direction the player was moving in.
//Really handy and determines the facing direction.
if(input.x != 0){
lastXPositionChange = input.x;
}
//Checks for input from a key.
if (Input.GetKeyDown(KeyCode.Space)){
//Calls a user-made method.
CmdFire(lastXPositionChange);
}
}
[Command]
void CmdFire(float sentLastXPositionChange){
//This is a float that adds or subtracts 0.7 to the vector so that the bullet does not spawn inside the player and kill them.
var bulletDirection = 0.8f;
//If facing left, make initial position of the bullet on the left.
if(sentLastXPositionChange < 0)
bulletDirection = -0.8f;
//Vector needed to determine which side the bullet needs to spawn in on.
//Needs to be vector so that it can be added with the other vector.
Vector3 bulletPosition = new Vector3 (bulletDirection,0,0);
//This next line creates an instance of the bullet.
//All of these next lines are all in one method that instantiates the bullet.
GameObject bullet = (GameObject)Instantiate(
bulletPrefab, //The asset that it will look like.
transform.position + bulletPosition, //Sets position of bullet on top of the player.
Quaternion.identity); //Quaternions represent rotation.
float bulletVelocity = 10;
//These two if statements make sure that the bullet fires in the correct direction.
if(sentLastXPositionChange < 0)
bulletVelocity = -10f;
if(sentLastXPositionChange > 0)
bulletVelocity = 10f;
//Determines the velocity of the bullet.
bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(bulletVelocity, 0);
//This will spawn the bullet on the other clients.
NetworkServer.Spawn(bullet);
//Bullet is destroyed after two seconds.
Destroy(bullet, 2.0f);
}
}
And this is the bullet script...
using UnityEngine;
public class Bullet : MonoBehaviour
{
//Method for the collisions.
void OnCollisionEnter2D(Collision2D collision){
//Object hit.
var hit = collision.gameObject;
//Tries to get the player that was hit. If there was a hit on some other object, then this is null.
var hitPlayer = hit.GetComponent<PlayerMove>();
/*With even the tiniest network lag, the bullet fired from one of the players can
actually hit the player firing it, even though the bulled spawns at one of the sides
of him and is faster than him. This if statement eliminates all doubt by asking if the
person hit is the same as the parent. This way a bullet can't ever hit the person who
fired it.
*/
//If the playerHit is the parent (DOESN'T WORK)
var parent = this.GetComponent<PlayerMove>();
Debug.Log(parent);
if (hitPlayer == parent){
Debug.Log("Hit parent!");
return;
}
//Destroy the object no matter what it hits. WALLS! PEOPLE! FLOORS!
if(hitPlayer != null){
//Hit is the game object. It will call its instance of combat, which has its health and other properties.
var combat = hit.GetComponent<Combat>();
//This will make the hit player take ten points of damage.
combat.TakeDamage(10);
//This will destroy the bullet.
Destroy(gameObject);
}
}
}
We know that the PlayerMove component of the bullet is null because we haven't parented the bullet with the PlayerMove, but we can't parent it because the bullet would move WITH the PlayerMove. It's just there so that you know where we're trying to put the working code. PLS HALP :'C
I am having the same problem, it is due to the delay in player position on client.Pls share the solution if you solve it.
Your answer
Follow this Question
Related Questions
Flipping Sprite on Client/Over Network 0 Answers
Flipping sprites in Multiplayer doesn't flip host 2 Answers
How can I animate a child sprite of a parent object? 0 Answers
Unity networking tutorial? 6 Answers
Batch mode vs no graphic card at all 0 Answers