The code is the same and I am following the lecture.
I don’t know what the problem is.
So the client is able to attack the host’s units? But the host won’t attack the client’s units?
Is it moving to attack, but not firing a projectile?
Well that is very strange, would you like to upload your project here and I can take a look?
https://gdev.tv/projectupload
I uploaded my project 12 hours ago
Are you getting any errors in the console when you host in the editor? I’m getting some errors when I open your project. But I opened it in Unity 2020. Also what version of Mirror are you using, I don’t see a version number listed in the project files.
my unity version is 2019.4.40f.
Also the mirror version I use is 29.2.7. The mirror that can be downloaded from the Asset Store supports Unity 2021 or later, so I got it separately from Github.
Okay, and are you getting any errors in the console?
I’m thinking it could be either a Mirror/Unity not playing well together issue, or an input system issue.
You can rule out the in put system issue by going to Project Settings → Player → Other and making sure the Active Input Handling is set to both. Sometimes there is a bug where Unity resets this so make sure it is correct if you are still getting the error. Let me know if this helps.
this is my console view.
Does this matter?
I don’t think that input manager warning is an issue. Is your input system set to both?
You need to go the Player tab there, under project settings. That is where the input system is set.
Project Settings → Player → Other and making sure the Active Input Handling is set to both.
oh…
Only one input system was checked. Let’s check again.
I’ve just checked. Still seeing the same thing.
Okay, just double check that changing the input system worked. There was bug in Unity 2019 that the setting would reset itself sometimes.
What happens if you play from two builds?
Okay, I says we try and resolve the null reference exception, just to rule that out as well.
You can do this by moving the player reference script into a coroutine and adding a small delay.
void Update()
{
StartCoroutine(GetPlayer());
private IEnumerator GetPlayer()
{
if (player == null)
{
yield return new WaitForSeconds(0.5f);
player = NetworkClient.connection.identity.GetComponent<RTSPlayer>();
}
}
It’s still the same thing.
Is there something I am missing in my code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class UnitProjectile : NetworkBehaviour
{
[SerializeField] Rigidbody rb = null;
[SerializeField] float destroyAfterSecond = 5f;
[SerializeField] float launchForce = 10f;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.forward * launchForce;
}
public override void OnStartServer()
{
Invoke(nameof(DestroySelf), destroyAfterSecond);
}
[Server]
void DestroySelf()
{
NetworkServer.Destroy(gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class UnitFiring : NetworkBehaviour
{
[SerializeField] Targeter targeter = null;
[SerializeField] GameObject projectilePrefab = null;
[SerializeField] Transform ShotPos = null;
[SerializeField] float fireRange = 5f;
[SerializeField] float fireRate = 1f;
[SerializeField] float rotationSpeed = 20f;
float lastFireTime;
[ServerCallback]
void Update()
{
Targetable target = targeter.GetTarget();
if (target== null) { return;}
if (!CanFireAtTarget()) { return; }
//유닛의 회전각
Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - transform.position);
//유닛의 회전속도
transform.rotation = Quaternion.RotateTowards(transform.rotation,targetRotation,rotationSpeed * Time.deltaTime);
//발사속도
if (Time.time > (1/fireRate) + lastFireTime)
{
Debug.Log("Shot");
//발사각
Quaternion projectileRot = Quaternion.LookRotation(target.GetAimPoint().position - ShotPos.position);
GameObject instance = Instantiate(projectilePrefab, ShotPos.position, projectileRot);
NetworkServer.Spawn(projectilePrefab, connectionToClient);
lastFireTime = Time.time;
}
}
[Server]
bool CanFireAtTarget()
{
return (targeter.GetTarget().transform.position - transform.position).sqrMagnitude <= fireRange * fireRange;
}
}
In the RTSPlayer.cs you are missing your subscription statements for the client under the OnStartClient() method.
public override void OnStartClient()
{
if (!isClientOnly) { return; }
Unit.AuthorityOnUnitSpawned += AuthorityHandleUnitSpawned;
Unit.AuthorityOnUnitDespawned += AuthorityHandleUnitDespawned;
}