Search Unity

A problem in Unity's Official Multiplayer Networking Tutorial

Discussion in 'Multiplayer' started by GreenGalaxyOnly, Oct 31, 2017.

  1. GreenGalaxyOnly

    GreenGalaxyOnly

    Joined:
    Jul 26, 2015
    Posts:
    4
    I followed the Unity's Multiplayer Networking tutorial, and everything was fine until, I got to the part 12.
    Player Health (Single Player).
    Under the Create Bullet Collisions: it said: "Now when a bullet hits another GameObject with a Collider attached, the bullet will be destroyed. Because the bullet is being managed by the NetworkManager, when the bullet on the Server is destroyed, it will be destroyed on all of the Clients as well."

    (https://unity3d.com/learn/tutorials...ng/player-health-single-player?playlist=29690)

    The problem is: The bullet does not Destroy itself on collision, but bounces off of the capsule collider that is attached to the players and gets destroyed (after the two seconds have passed). :confused:
    More info:

    • Both the player and the Bullet prefabs have colliders not checked as triggers. (They are also bigger than their model's mesh...)
    • Only the bullet has a rigidbody attached to it.
    • Both the player and the bullet prefabs have the "Network Identity" and "Network Transform" attached to them.
    Aka: everything is just like it should be if you follow the tutorial...:cool:
    1.The Bullet Script.
    2.The PlayerController Script.

    Bullet:
    using System.Collections;
    using UnityEngine;

    public class Bullet : MonoBehaviour
    {

    void OnColisionEnter()
    {
    Debug.Log ("The bullet just hit the object");
    Destroy (gameObject);
    }
    }


    PlayerController:

    using UnityEngine;
    using UnityEngine.Networking;

    public class PlayerController : NetworkBehaviour
    {
    public GameObject bulletPrefab;
    public Transform bulletSpawn;



    void Update()
    {

    if (!isLocalPlayer){

    return;

    }

    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

    transform.Rotate(0, x, 0);
    transform.Translate(0, 0, z);

    if (Input.GetKeyDown(KeyCode.Space))
    {
    CmdFire();
    }
    }

    public override void OnStartLocalPlayer()
    {
    GetComponent<MeshRenderer>().material.color = Color.blue;
    }

    [Command]
    void CmdFire()
    {
    Rigidbody rg;

    // Create the Bullet from the Bullet Prefab
    var bullet = (GameObject)Instantiate (
    bulletPrefab,
    bulletSpawn.position,
    bulletSpawn.rotation);

    // Add velocity to the bullet
    bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 6;

    // Spawn the bullet on the Clients
    NetworkServer.Spawn(bullet);

    // Destroy the bullet after 2 seconds
    Destroy(bullet, 2.0f);
    }

    }


    If you need more information, I will gladly help.:)
    I spent more than six ours on figuring out the problem here, hopefully it's not just Unity's fault for not updating the tutorial page.

    This is my first time posting on the forum so please forgive me if I did something wrong...(also, English is not my first language) though I will understand you...
    Anyway, I will try to Check this page every day.;)
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your code is painful to read without proper formatting. Use code tags on the forum.

    You misspelled OnCollisionEnter. If it is not spelled correctly it won't automatically be called when a collision occurs. So try updating that and then see if everything works :)
     
    GreenGalaxyOnly likes this.
  3. GreenGalaxyOnly

    GreenGalaxyOnly

    Joined:
    Jul 26, 2015
    Posts:
    4
    Man, I'm so sorry!:oops:
    I made the stupid spelling mistake, and took so much time.
    You don't know how much I appreciate your help!:D
    If not you then I would spend next two weeks figuring it out, and maybe just dropping from that tutorial.:(
    Thank you a lot!:)

    Do I need to close this post or something?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Don't worry man, it happens! When you get stuck it often helps to get some fresh eyes on the problem, or walk away and come back later to it.

    You don't need to close anything. Eventually it will just disappear down the list of threads :p