Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by kickedmidget · Oct 02, 2016 at 05:41 AM · networking2d-platformer

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image surajsirohi1008 · Aug 16, 2017 at 02:37 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges