From 69ff29badc94a957c221d2b5f87a43a665cda7ed Mon Sep 17 00:00:00 2001 From: JWS Date: Fri, 28 Jan 2022 19:33:32 -0600 Subject: [PATCH] More cleanup and removal of cruft --- Assets/CameraFollowRocket.cs | 3 +- Assets/RockField1.prefab | Bin 8484 -> 8484 bytes Assets/RockField2.prefab | Bin 8484 -> 8484 bytes Assets/RocketSphere.cs | 66 ++++++++--------------------------- 4 files changed, 16 insertions(+), 53 deletions(-) diff --git a/Assets/CameraFollowRocket.cs b/Assets/CameraFollowRocket.cs index 82386f5..98341f2 100644 --- a/Assets/CameraFollowRocket.cs +++ b/Assets/CameraFollowRocket.cs @@ -52,6 +52,7 @@ public static float ClampAngle(float angle, float min, float max) // this camera repositioning will get overridden with any VR camera update if it is active and tracking void LateUpdate() { + if (!player) return; if (player.transform && player.gameObject.activeSelf) { @@ -63,7 +64,7 @@ void LateUpdate() { #if !UNITY_ANDROID || UNITY_EDITOR // Read the mouse input axis - rotationX = rotationX + Input.GetAxis("Mouse X") * sensitivityX; + rotationX += Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY; rotationX = ClampAngle(rotationX, minimumX, maximumX); diff --git a/Assets/RockField1.prefab b/Assets/RockField1.prefab index d48bef1ca59f24f8f3fe73c16c0dea46d2039c48..36679a1840fecba4cfcec8e53c3b74f921fd94cd 100644 GIT binary patch delta 14 VcmZ4Dw8Uw{1$jos%@^f^838Tg1!VvL delta 14 VcmZ4Dw8Uw{1$joU%@^f^838UB1#SQU diff --git a/Assets/RockField2.prefab b/Assets/RockField2.prefab index c2589a0519c8509481db5d571e6cb880f38a0a8e..58258793ce9e97e8dddd5e8057eadb4cdc33aa8d 100644 GIT binary patch delta 14 VcmZ4Dw8Uw{1$jos%@^f^838Tg1!VvL delta 14 VcmZ4Dw8Uw{1$joU%@^f^838UB1#SQU diff --git a/Assets/RocketSphere.cs b/Assets/RocketSphere.cs index 0d5c359..9138079 100644 --- a/Assets/RocketSphere.cs +++ b/Assets/RocketSphere.cs @@ -30,7 +30,6 @@ public class RocketSphere : NetworkBehaviour { GameObject rocket; [SyncVar] float radius = 10; - [SyncVar] bool visible = false; public float rotationSpeed; public float forwardSpeed; public GameObject shotPrefab; @@ -44,8 +43,7 @@ public class RocketSphere : NetworkBehaviour public GameObject explosionPrefab; public GameObject spawnPrefab; Rigidbody rb; - [SyncVar(hook = "OnChangeColour")] - Color RocketColor = Color.white; + [SyncVar] Color RocketColor = Color.white; // Unity makes a clone of the Material every time GetComponent().material is used. // Cache it here and Destroy it in OnDestroy to prevent a memory leak. @@ -63,9 +61,6 @@ public override void OnStartServer() // create a random color for each player as they are created on the server RocketColor = new Color(Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f), Random.Range(0.3f, 1.0f)); - - // need to maintain this separately so we can sync state on client connect with existing objects - visible = false; } // Use this for initialization @@ -85,6 +80,7 @@ void Start() // Find the rocket child object rocket = transform.Find("Rocket").gameObject; + rocket.transform.position = pos; rocket.transform.rotation = rot; @@ -107,35 +103,12 @@ void Start() Camera.main.GetComponent().player = transform.gameObject.transform.Find("Rocket").gameObject.transform; // update the color to match the color on the server - ChangeColour(); - - // spawn the ship on the server - CmdSpawnShipDelay(); - } - - void ChangeColour() - { if (cachedMaterial == null) cachedMaterial = rocket.GetComponent().material; - cachedMaterial.color = RocketColor; - } - - void OnChangeColour(Color oldColor, Color newColor) - { - CmdChangeColour(); - } - - [ClientRpc] - void RpcChangeColour() - { - ChangeColour(); - } - [Command] - void CmdChangeColour() - { - RpcChangeColour(); + // spawn the ship on the server + CmdSpawnShipDelay(); } private void OnLevelWasLoaded(int level) @@ -151,10 +124,12 @@ void MySetActive(bool active, Quaternion rotation) { // stop the ship from moving due to rb momentum rb.isKinematic = true; + // set player ship to be stopped rb.velocity = Vector3.zero; + + // make the rocket invisible rocket.SetActive(false); - visible = false; } else { @@ -164,8 +139,9 @@ void MySetActive(bool active, Quaternion rotation) // restart rb movement rb.isKinematic = false; + // make the rocket visible again rocket.SetActive(true); - visible = true; + // play the hyperspace sound on enable hyperspaceSound.Play(); } @@ -189,13 +165,11 @@ void CmdSpawnShipDelay() rpcSpawnShipDelay(); } + [Client] void SpawnShip() { if (!isLocalPlayer) return; - // make sure the color is correct, might not need this - CmdChangeColour(); - // re-enable this rocket on all clients through server at the direction the user is looking at the moment CmdMySetActive(true, Camera.main.transform.rotation); } @@ -219,11 +193,6 @@ void OnTriggerEnter(Collider other) GameObject explosion = Instantiate(explosionPrefab, rocket.transform.position, Quaternion.identity) as GameObject; NetworkServer.Spawn(explosion); - // stop the ship from moving due to rb momentum - //rb.isKinematic = true; - // set player ship to be stopped - //rb.velocity = Vector3.zero; - // deactivate the ship on all clients RpcMySetActive(false, Quaternion.identity); @@ -247,13 +216,12 @@ void Fire() Vector3 torque = Vector3.Cross(transform.rotation * Vector3.right, transform.rotation * Vector3.forward); rbshot.AddTorque(torque.normalized * (shotSpeed / radius)); - //rbshot.velocity = torque.normalized * (shotSpeed / radius); - // need to rotate the shot just in front of the rocket + // need to rotate the shot just in front of the rocket so we don't run into our own shot Quaternion turn = Quaternion.Euler(0f, -50f/radius, 0f); rbshot.MoveRotation(rbshot.rotation * turn); - // Fire the shot locally do not spawn on server + // Fire the shot on the server by spawning it NetworkServer.Spawn(shot); } @@ -371,25 +339,19 @@ void CmdHyperspace() void Update() { - //if (!rocket) return; - - if (rocket.activeSelf != visible) - { - // we are out of sync with the server - rocket.SetActive(visible); - } - // only allow input for local player if (!isLocalPlayer) return; // ignore input while destroyed if (!rocket.activeSelf) return; + // left and right rotation is controlled by the horizontal joystick axis float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime; Quaternion turn = Quaternion.Euler(0f, 0f, -rotation); rb.MoveRotation(rb.rotation * turn); + // forward momentum is controlled by the verticle joystick axis float forward = Input.GetAxis("Vertical"); if (forward > 0) {