- Home /
Question by
iveL_newO · May 15, 2018 at 06:10 AM ·
networkingclientcommands
[Command] not being called by clients
Here is my complete code, if you don't want to read it all I the more important parts are at the bottom. using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine.Networking;
[AddComponentMenu("Camera/Simple Smooth Mouse Look ")]
public class playerController : NetworkBehaviour
{
//this isn't mine (Don't edit)
Vector2 _mouseAbsolute;
Vector2 _smoothMouse;
public Vector2 clampInDegrees = new Vector2(360, 180);
public bool lockCursor;
public Vector2 sensitivity = new Vector2(2, 2);
public Vector2 smoothing = new Vector2(3, 3);
public Vector2 targetDirection;
//from here on is mine
public Transform player;
public int cubes = 0;
public int textNum = 10;
public float speed = 15;
public float shootSpeed = 10;
public float timer = 0;
public UnityEngine.Transform sameBlock = null;
public bool playing;
public bool blueTrue;
public bool dontGo;
public UnityEngine.UI.Text text;
public Material red;
public GameObject playButton;
public GameObject blueButton;
public GameObject redButton;
public GameObject canvas;
public Material redBox;
public Material blueBox;
Camera cam;
Object prefab;
Object canvasPrefab;
void Start(){
playing = false;
player = this.transform;
canvasPrefab = Resources.Load ("Canvas");
canvas = Instantiate (canvasPrefab) as GameObject;
if (isLocalPlayer) {
canvas.transform.SetParent (player);
} else {
Destroy (canvas);
}
playButton = canvas.transform.GetChild (2).gameObject;
playButton.GetComponent<Button> ().onClick.AddListener (play);
blueButton = canvas.transform.GetChild (4).gameObject;
blueButton.SetActive (false);
blueButton.GetComponent<Button> ().onClick.AddListener (blueTeam);
redButton = canvas.transform.GetChild (3).gameObject;
redButton.SetActive (false);
redButton.GetComponent<Button> ().onClick.AddListener (redTeam);
text = canvas.transform.GetChild (5).GetComponent<UnityEngine.UI.Text>();
text.text = textNum.ToString();
prefab = Resources.Load ("cube") as GameObject;
cam = GetComponent<Camera>() ;
if (!isLocalPlayer) {
cam.enabled = false;
}
//not mine (Don't edit)
targetDirection = transform.localRotation.eulerAngles;
}
public void play(){
playButton.SetActive (false);
redButton.SetActive (true);
blueButton.SetActive (true);
}
public void blueTeam(){
Cursor.visible = false;
playing = true;
blueButton.SetActive (false);
redButton.SetActive (false);
blueTrue = true;
RenderSettings.skybox = blueBox;
}
public void redTeam(){
Cursor.visible = false;
playing = true;
blueButton.SetActive (false);
redButton.SetActive (false);
blueTrue = false;
player.GetComponent<MeshRenderer> ().material = red;
RenderSettings.skybox = redBox;
}
void Break(){
if (Time.unscaledTime - timer > 0.5f) {
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
if (sameBlock.name == hit.transform.name) {
if (hit.transform.tag == "cube") {
CmdNetDestroy (GameObject.Find (hit.transform.name));
timer = Time.unscaledTime - 0.25f;
dontGo = true;
textNum++;
sameBlock = null;
}
} else {
timer = Time.unscaledTime - 0.25f;
Same ();
}
}
}
}
return;
}
void CmdNetDestroy(GameObject obj){
print ("DESTROYED");
Destroy (obj);
}
void Place(){
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
timer = 0.0f;
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
GameObject cube = Instantiate (prefab) as GameObject;
cube.name = "cube " + cubes.ToString ();
cubes++;
cube.transform.position = hit.point + Vector3.up * 0.5f;
if (!blueTrue) {
cube.GetComponent<MeshRenderer> ().material = red;
}
textNum += -1;
//spawning the cube
CmdNetSpawn(cube);
}
}
}
[Command]
void CmdNetSpawn(GameObject obj){
print ("PLACED");
NetworkServer.Spawn (obj);
}
void Same(){
shootSpeed = 0;
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
sameBlock = hit.transform;
}
}
}
void Shoot(){
print ("SHOT");
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
Rigidbody rigid = hit.transform.GetComponent<Rigidbody> ();
if (rigid != null && sameBlock.name == hit.transform.name) {
CmdNetMove (player.transform.forward, shootSpeed, rigid);
shootSpeed = 10;
}
}
}
}
[Command]
void CmdNetMove(Transform player, float speed, Rigidbody rigid){
rigid.AddForce (player.forward * speed, ForceMode.Impulse);
}
void Update(){
if (!isLocalPlayer){
return;
}
if (playing) {
print ("playing");
text.text = textNum.ToString ();
//this isn't my code (Don't edit)
if (lockCursor) {
Cursor.lockState = CursorLockMode.Locked;
}
var targetOrientation = Quaternion.Euler (targetDirection);
var mouseDelta = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
mouseDelta = Vector2.Scale (mouseDelta, new Vector2 (sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
_smoothMouse.x = Mathf.Lerp (_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
_smoothMouse.y = Mathf.Lerp (_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
_mouseAbsolute += _smoothMouse;
if (clampInDegrees.x < 360)
_mouseAbsolute.x = Mathf.Clamp (_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
if (clampInDegrees.y < 360)
_mouseAbsolute.y = Mathf.Clamp (_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
transform.localRotation = Quaternion.AngleAxis (-_mouseAbsolute.y, targetOrientation * Vector3.right) * targetOrientation;
var yRotation = Quaternion.AngleAxis (_mouseAbsolute.x, transform.InverseTransformDirection (Vector3.up));
transform.localRotation *= yRotation;
//from here on is my code
if (Input.GetMouseButtonDown (1)) {
timer = Time.unscaledTime;
dontGo = false;
Same ();
}
if (Input.GetMouseButton (1)) {
if (sameBlock == null) {
Same ();
}
Break ();
}
if (Input.GetMouseButtonUp (1) && textNum > 0 && !dontGo) {
sameBlock = null;
Place ();
}
if (Input.GetMouseButton (0) && shootSpeed < 50) {
shootSpeed += 2;
}
if (Input.GetMouseButtonDown (0)) {
Same ();
}
if (Input.GetMouseButtonUp (0)) {
Shoot ();
}
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
float up = 0;
if (Input.GetKey (KeyCode.Space)) {
up = 1;
} else {
if (Input.GetKey (KeyCode.LeftShift)) {
up = -1;
}
}
player.position += v * player.forward * Time.deltaTime * speed;
player.position += h * player.right * Time.deltaTime * speed;
player.position += up * Vector3.up * Time.deltaTime * speed;
if (player.position.y < 0.4f) {
Vector3 vect = player.position;
vect.y = 0.4f;
player.position = vect;
}
}
}
Here are the parts where I call commands
void Break(){
if (Time.unscaledTime - timer > 0.5f) {
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
if (sameBlock.name == hit.transform.name) {
if (hit.transform.tag == "cube") {
CmdNetDestroy (GameObject.Find (hit.transform.name));
timer = Time.unscaledTime - 0.25f;
dontGo = true;
textNum++;
sameBlock = null;
}
} else {
timer = Time.unscaledTime - 0.25f;
Same ();
}
}
}
}
return;
}
void CmdNetDestroy(GameObject obj){
print ("DESTROYED");
Destroy (obj);
}
void Place(){
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
timer = 0.0f;
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
GameObject cube = Instantiate (prefab) as GameObject;
cube.name = "cube " + cubes.ToString ();
cubes++;
cube.transform.position = hit.point + Vector3.up * 0.5f;
if (!blueTrue) {
cube.GetComponent<MeshRenderer> ().material = red;
}
textNum += -1;
//spawning the cube
CmdNetSpawn(cube);
}
}
}
[Command]
void CmdNetSpawn(GameObject obj){
print ("PLACED");
NetworkServer.Spawn (obj);
}
void Shoot(){
print ("SHOT");
RaycastHit hit;
Ray ray = cam.ScreenPointToRay (new Vector3 (Screen.width / 2, Screen.height / 2, 0));
if (Physics.Raycast (ray, out hit, 100.0f)) {
if (hit.transform) {
Rigidbody rigid = hit.transform.GetComponent<Rigidbody> ();
if (rigid != null && sameBlock.name == hit.transform.name) {
CmdNetMove (player.transform.forward, shootSpeed, rigid);
shootSpeed = 10;
}
}
}
}
[Command]
void CmdNetMove(Transform player, float speed, Rigidbody rigid){
rigid.AddForce (player.forward * speed, ForceMode.Impulse);
}
All the parts were I say the code is not mine are because I used a first-person script that I did not write.
Comment
Your answer
Follow this Question
Related Questions
Object not updating on host 1 Answer
Command not executing for all clients 0 Answers