- Home /
Question by
howtobasic100legit · Mar 12, 2019 at 03:46 PM ·
prefabmultiplayerscaleprojectiletop down shooter
How do I scale my prefab over the network?
I'm trying to make a multiplayer top-down shooter where the player has to charge the projectiles(scale and speed) before shooting them. Everything works by now with one exception. Only the host can see the scale of the projectiles.
PlayerController
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour { public float moveSpeed; private Vector3 moveInput; private Vector3 moveVelocity; private Rigidbody playerRigidbody;
public Material localMaterial;
public Transform firePoint;
public GameObject playerProjectile;
private GameObject projectile;
public float chargeSpeed = 10;
public float chargeLimit = 30;
public bool isCharging;
public float newCharge;
private float myCharge;
private float chargeLevel = 0f;
private float newScale;
private Camera mainCamera;
void Start()
{
mainCamera = FindObjectOfType<Camera>();
playerRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (!isLocalPlayer)
{
return;
}
moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
moveVelocity = moveInput * moveSpeed;
if (Input.GetMouseButtonDown(0))
{
CmdSpawn();
}
if (Input.GetMouseButton(0))
{
CmdCharge();
}
if (Input.GetMouseButtonUp(0))
{
CmdResetCharge();
}
Ray cameraRay = mainCamera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
Debug.DrawLine(cameraRay.origin, pointToLook, Color.cyan);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
public override void OnStartLocalPlayer()
{
GetComponent<MeshRenderer>().material = localMaterial;
}
private void FixedUpdate()
{
playerRigidbody.velocity = moveVelocity;
}
private float StartCharging()
{
chargeLevel += Time.deltaTime * chargeSpeed;
chargeLevel = (chargeLevel > chargeLimit ? chargeLimit : chargeLevel + Time.deltaTime * chargeSpeed);
return chargeLevel;
}
[Command]
void CmdSpawn()
{
projectile = GameObject.Instantiate(playerProjectile, firePoint.position, transform.rotation);
projectile.GetComponent<PlayerProjectileCotroller>().isCharging = true;
NetworkServer.Spawn(projectile);
}
[Command]
void CmdCharge()
{
newCharge = StartCharging();
newScale = newCharge * 0.1f;
projectile.transform.localScale = new Vector3(newScale, newScale, newScale);
projectile.GetComponent<Rigidbody>().velocity = gameObject.transform.forward * newCharge;
print(newCharge);
isCharging = true;
projectile.GetComponent<PlayerProjectileCotroller>().transform.position = firePoint.position;
projectile.GetComponent<PlayerProjectileCotroller>().transform.rotation = transform.rotation;
}
[Command]
void CmdResetCharge()
{
chargeLevel = 0;
isCharging = false;
projectile.GetComponent<PlayerProjectileCotroller>().isCharging = false;
}
}
PlayerProjectileController
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;
public class PlayerProjectileCotroller : NetworkBehaviour {
public bool isCharging;
[SyncVar(hook = "OnSetScale")] public Vector3 scale;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isCharging == true)
{
}
if (isCharging == false)
{
}
}
private void OnSetScale(Vector3 vec)
{
this.scale = vec;
this.transform.localScale = vec;
}
}
Comment