Hi,
I’m trying to get the basics of networking down for a simple game I’ve made. I followed the online unity tutorial that teaches you how to do so.
Currently, my client and host control properly in their movement (host controls host, client controls client).
As soon as I tried to apply the info in the tutorial to other aspects of my game (spawning things, jumping, etc), things got super wacky.
My problems boils down to… when I add the [Command] part into my code, it simply doesnt send to the server
I’ve made a gif for you to visualize these problems:
I’m showing here that
a) though the client and host move independent of each other, the direction of both characters change on the host, and neither change on the client
b) when a teleport ball is shot, it spawns from both players, though this is only seen on the host, and the client character is unaffected by it
c) The host can only jump once, due to groundcheck rules. The client ignores the groundcheck.
Here are screenshots of my network components:
And as for the teleball script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Nawlins : NetworkBehaviour {
PlayerController _pc;
Transform playerTransform;
Camera viewCamera;
//Skills?
public GameObject projectileOne;
public float projectileOneForce;
public GameObject projectileTwo;
public float projectileTwoForce;
public float swipeCooldown;
public float swipeVelocity;
float swipeAvailableAt;
public float teleBallCooldown;
float teleBallAvailableAt;
// Use this for initialization
void Start () {
_pc = GetComponent<PlayerController>();
viewCamera = _pc.viewCamera;
playerTransform = _pc.playerTransform;
swipeAvailableAt = 0.0F;
teleBallAvailableAt = 0.0F;
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
return;
}
CmdClickListen();
}
[Command]
void CmdClickListen()
{
if (Input.GetMouseButtonDown(0) && swipeAvailableAt < Time.time) //Nawlins' left click is a dash/swipe
{
swipeAvailableAt = Time.time + swipeCooldown;
int direction;
if (_pc.isFacingRight)
{
direction = 1;
}else
{
direction = -1;
}
_pc.rb.AddForce(Vector2.right * direction * swipeVelocity);
//_pc.rb.velocity = Vector2.right * direction * swipeVelocity;
}
if (Input.GetMouseButtonDown(1) && teleBallAvailableAt < Time.time) //right click
{
teleBallAvailableAt = Time.time + teleBallCooldown;
Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);
Vector3 clickDirection = (ray.origin - transform.position);
clickDirection.z = 0;
Vector3 normal = clickDirection.normalized;
GameObject clone = (GameObject)Instantiate(projectileTwo, transform.position, transform.rotation);
clone.layer = 11;
clone.GetComponent<Projectile>().owner = playerTransform;
clone.GetComponent<Rigidbody2D>().AddForce(normal * projectileTwoForce);
NetworkServer.Spawn(clone);
}
}
}