Help with Photon Networking Dodge Ball Game
My game requires players to pick up balls by parenting the object to the players hand and changing their positions + throwing them by adding force to the balls rigidbody.
I need help with making the balls sync over the network. Is there a easier way to just change the position of the ball for all players and sync the rigidbody? How will i go about this?
Heres the ThrowΠckUp Code: using UnityEngine; using System.Collections;
public class Thowing : MonoBehaviour { public float ballHoldTimer = 5; float ballTimer; int ammo = 0;
[SerializeField] private Camera playerCamera;
[SerializeField] public LayerMask mask; public float maxThrowForce = 2000; public float throwForce = 2000; public float pickUpRange = 3; public GameObject ball; public Transform ballSpawnPoint;
bool canShoot = false; bool canCatch = true;
Animator anim;
public GameObject arms;
GameObject thrownBall;
void SetDefaults() { ballTimer = ballHoldTimer; anim = arms.GetComponent (); }
void Start() { SetDefaults (); }
void Update () { if (Input.GetKeyDown (KeyCode.Mouse0) && canShoot) { Throw (); }
if (Input.GetKeyDown (KeyCode.Mouse1) && canShoot) { Block (); }
if (Input.GetKeyDown (KeyCode.Alpha1)) { throwForce = maxThrowForce; }
if (Input.GetKeyDown (KeyCode.Alpha2)) { throwForce = maxThrowForce/2; }
if (Input.GetKeyDown (KeyCode.G)) { float lastTF = throwForce; throwForce = 5; Throw (); throwForce = lastTF; }
if (Input.GetKeyDown (KeyCode.E)) { Shoot(); StartCoroutine (GrabAnim()); }
if (Input.GetKeyDown (KeyCode.F)) { StartCoroutine (FakeAnim()); }
if (ammo >= 1) { ammo = 1; }
if (ammo <= 0) { ammo = 0; }
if (ammo == 1) { canShoot = true; } else { canShoot = false; }
if (ballTimer <= 0 && canShoot) { Throw (); ballTimer = 0; Debug.Log ("You were holding a ball too long!"); } }
void Shoot() { RaycastHit hit; if (Physics.Raycast (playerCamera.transform.position, playerCamera.transform.forward, out hit, pickUpRange, mask)) { if (hit.collider.tag == "Ball" && canCatch == true) { StartCoroutine (GrabAnim ()); ballTimer = 0; ballTimer += ballHoldTimer; ammo += 1; thrownBall = hit.collider.gameObject; thrownBall.GetComponent ().isKinematic = true; thrownBall.transform.position = ballSpawnPoint.position; thrownBall.transform.rotation = ballSpawnPoint.rotation; thrownBall.gameObject.transform.SetParent (ballSpawnPoint); thrownBall.layer = LayerMask.NameToLayer ("Weapon"); thrownBall.GetComponent ().enabled = false; Debug.Log ("Picked Up Ball"); } } }
IEnumerator GrabAnim() { if (canCatch == true) { anim.SetTrigger ("Grab"); canCatch = false; yield return new WaitForSeconds (0.5f); canCatch = true; StopCoroutine ("GrabAnim"); } }
IEnumerator FakeAnim() { if (canCatch == true) { anim.SetTrigger ("Throw"); canCatch = false; yield return new WaitForSeconds (0.1f); canCatch = true; StopCoroutine ("FakeAnim"); } }
IEnumerator ThrowAnim() { anim.SetTrigger ("Throw"); yield return new WaitForSeconds (0.20f); StopCoroutine ("ThrowAnim"); }
void FixedUpdate() { if (canShoot) { ballTimer -= 1; } else { ballTimer = 0; } }
void Throw() { thrownBall.gameObject.transform.SetParent(null); thrownBall.layer = LayerMask.NameToLayer ("Ball"); thrownBall.GetComponent ().isKinematic = false; thrownBall.GetComponent().AddForce (thrownBall.transform.forward * throwForce); thrownBall.GetComponent ().enabled = true; StartCoroutine (ThrowAnim()); ammo -= 1; }
void Block() {
}
void OnTriggerEnter(Collider col) { if (col.tag == "Ball") {
} }
}
Your answer
Follow this Question
Related Questions
Instantiate with Photon 0 Answers
Photon RPC Only Working Halfway 1 Answer
Client Disconnect on Photon Server using MLAPI 0 Answers
Photon ccu question 0 Answers
Unity Photon Player Instantiation 1 Answer