Search Unity

Tanks Multiplayer error

Discussion in 'Multiplayer' started by alexdion, Jul 11, 2017.

  1. alexdion

    alexdion

    Joined:
    Jul 11, 2017
    Posts:
    3
    Hi. I'm a beginner in Unity but I finished my first game through the Unite 2015 Boston. I'm trying to turn it into Multiplayer and it's almost all working except when I start in a Multiplayer game and I press the variable range shot, my tank gets looped and gets stuck. Has anyone here been through this? Here is the part of the code that is in error ...


    using UnityEngine;
    using System.Collections;
    using UnityEngine.Networking;

    public class TankShooting_Net : NetworkBehaviour
    {
    public int m_PlayerNumber = 1; // Used to identify the different players.
    public GameObject m_Shell; // Prefab of the shell.
    //public Slider m_AimSlider; // A child of the tank that displays the current launch force.
    public AudioSource m_ShootingAudio; // Reference to the audio source used to play the shooting audio. NB: different to the movement audio source.
    public AudioClip m_ChargingClip; // Audio that plays when each shot is charging up.
    public AudioClip m_FireClip; // Audio that plays when each shot is fired.
    public float m_MinLaunchForce = 15f; // The force given to the shell if the fire button is not held.
    public float m_MaxLaunchForce = 30f; // The force given to the shell if the fire button is held for the max charge time.
    public float m_MaxChargeTime = 0.75f; // How long the shell can charge for before it is fired at max force.


    private string m_FireButton; // The input axis that is used for launching shells.
    private float m_CurrentLaunchForce; // The force that will be given to the shell when the fire button is released.
    private float m_ChargeSpeed; // How fast the launch force increases, based on the max charge time.
    private bool m_Fired; // Whether or not the shell has been launched with this button press.

    [SerializeField] float power = 800f;
    [SerializeField] Transform gunBarrel;

    void Reset()
    {
    //Pega a localização do canhão
    gunBarrel = transform.Find("GunBarrel");
    }

    private void OnEnable()
    {
    // When the tank is turned on, reset the launch force and the UI
    m_CurrentLaunchForce = m_MinLaunchForce;
    //m_AimSlider.value = m_MinLaunchForce;
    }


    private void Start()
    {
    // The fire axis is based on the player number.
    m_FireButton = "Fire1";

    // The rate that the launch force charges up is the range of possible forces by the max charge time.
    m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
    }


    private void Update()
    {
    if (!isLocalPlayer)
    return;

    // The slider should have a default value of the minimum launch force.
    //m_AimSlider.value = m_MinLaunchForce;

    // If the max force has been exceeded and the shell hasn't yet been launched...
    if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
    {
    // ... use the max force and launch the shell.
    m_CurrentLaunchForce = m_MaxLaunchForce;
    CmdFire();
    }
    // Otherwise, if the fire button has just started being pressed...
    else if (Input.GetButtonDown(m_FireButton))
    {
    // ... reset the fired flag and reset the launch force.
    m_Fired = false;
    m_CurrentLaunchForce = m_MinLaunchForce;

    // Change the clip to the charging clip and start it playing.
    m_ShootingAudio.clip = m_ChargingClip;
    m_ShootingAudio.Play();
    }
    // Otherwise, if the fire button is being held and the shell hasn't been launched yet...
    else if (Input.GetButton(m_FireButton) && !m_Fired)
    {
    // Increment the launch force and update the slider.
    m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;

    // m_AimSlider.value = m_CurrentLaunchForce;
    }
    // Otherwise, if the fire button is released and the shell hasn't been launched yet...
    else if (Input.GetButtonUp(m_FireButton) && !m_Fired)
    {
    // ... launch the shell.
    CmdFire();
    }
    }

    [Command]
    void CmdFire()
    {
    // Set the fired flag so only Fire is only called once.
    m_Fired = true;

    GameObject shellInstance = Instantiate(m_Shell, gunBarrel.position,
    gunBarrel.rotation) as GameObject;

    shellInstance.GetComponent<Rigidbody>().velocity = m_CurrentLaunchForce * gunBarrel.forward;

    // Change the clip to the firing clip and play it.
    m_ShootingAudio.clip = m_FireClip;
    m_ShootingAudio.Play();

    // Reset the launch force. This is a precaution in case of missing button events.
    m_CurrentLaunchForce = m_MinLaunchForce;

    NetworkServer.Spawn(shellInstance);
    }
    }
     
  2. FatherW

    FatherW

    Joined:
    Jul 17, 2017
    Posts:
    1
    I got a same problem.
    When i build the game, server works perfect but client cannot change the AimSlider.value to LaunchForce value.
    Once i hold the space out of the max AimSlider, shell exploding at the mouse of the tank. and unstoppable instantiate shells.
    if you delete aim slider, launch the update part like this. the game can work without aim slider
    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();
    }
    }

    if you got the answer, plz let me know.
     
  3. alexdion

    alexdion

    Joined:
    Jul 11, 2017
    Posts:
    3
    Hi FatherW,
    I gave up and created a new way of coding by removing the shots with variable range, leaving the game simpler and consequently the problem was solved. If you want I can give you the code I created, but now the shots are always performed with fixed range. Hugs.
    Sorry for my poor English, i'm brazilian and i use google translator.