Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions Basic/2DSpaceShooter/Assets/Scripts/ShipControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Color GetColor(BuffType bt)
public class ShipControl : NetworkBehaviour
{
static string s_ObjectPoolTag = "ObjectPool";

NetworkObjectPool m_ObjectPool;
public GameObject BulletPrefab;
public AudioSource fireSound;
Expand Down Expand Up @@ -62,17 +62,17 @@ public class ShipControl : NetworkBehaviour
public ParticleSystem friction;
public ParticleSystem thrust;

float m_FrictionStopTimer = 0;
private NetworkVariableFloat m_FrictionEffectStartTimer = new NetworkVariableFloat(-10);

// for client movement command throttling
float m_OldMoveForce = 0;
float m_OldSpin = 0;

// server movement
private NetworkVariableFloat m_Thrusting = new NetworkVariableFloat();

float m_Spin;

Rigidbody2D m_Rigidbody2D;

void Awake()
Expand All @@ -99,7 +99,6 @@ void OnDisable()

void Start()
{
friction.Stop();
thrust.Stop();

DontDestroyOnLoad(gameObject);
Expand All @@ -114,8 +113,7 @@ public override void OnNetworkSpawn()
public void TakeDamage(int amount)
{
Health.Value = Health.Value - amount;
friction.Play();
m_FrictionStopTimer = Time.time + 1.0f;
m_FrictionEffectStartTimer.Value = NetworkManager.LocalTime.TimeAsFloat;

if (Health.Value <= 0)
{
Expand All @@ -142,20 +140,20 @@ void Fire(Vector3 direction)
}

bool bounce = BounceTimer.Value > Time.time;

GameObject bullet = m_ObjectPool.GetNetworkObject(BulletPrefab).gameObject;
bullet.transform.position = transform.position + direction;

var bulletRb = bullet.GetComponent<Rigidbody2D>();

var velocity = m_Rigidbody2D.velocity;
velocity += (Vector2)(direction) * 10;
bulletRb.velocity = velocity;
bullet.GetComponent<Bullet>().Config(this, damage, bounce, bulletLifetime);

bullet.GetComponent<NetworkObject>().Spawn(null, true);
}

void Update()
{
if (IsServer)
Expand Down Expand Up @@ -224,16 +222,36 @@ void UpdateServer()
}
}

void UpdateClient()
private void HandleFrictionGraphics()
{
if (!IsLocalPlayer)
var time = NetworkManager.ServerTime.Time;
var start = m_FrictionEffectStartTimer.Value;

bool frictionShouldBeActive = time >= start && time < start + 1f; // 1f is the duration of the effect

if (frictionShouldBeActive)
{
return;
if (friction.isPlaying == false)
{
friction.Play();
}
}
else
{
if (friction.isPlaying)
{
friction.Stop();
}
}
}

if (m_FrictionStopTimer < Time.time)
void UpdateClient()
{
HandleFrictionGraphics();

if (!IsLocalPlayer)
{
friction.Stop();
return;
}

// movement
Expand Down