- Home /
Getting UNET non-player actions to behave the same?
Hi, so I'm making a multiplayer fighting game and have gotten stuck trying to get bullets to shoot towards the aimer (a player controlled cylinder on the ground). The project is set up so that a player character spawns at Lobby join, and a seperate aimer GameObject with no NetworkIdentity spawns as well. With the code below, the host acts as it should, but the joined player is not finding it's aimer, and so isn't running the second half of the CmdBallAttackFire code. Does anyone know how to pull Client-only information onto the server, or see how to fix this?
Attached to Player....
public class PlayerMovement : NetworkBehaviour {
public float moveSpeed = 5f;
public GameObject aimer;
public GameObject aimerVec;
public float aimerMoveSpeed= 10f;
public float jumpHeight = 8f;
public float jumpsRemaining = 2f;
// Use this for initialization
void Start () {
if (isLocalPlayer)
{
Instantiate(aimer);
}
// NetworkServer.Spawn(aimer);
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
return;
}
//Player Movement
Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
GetComponent<Rigidbody>().MovePosition(transform.position + moveDirection * moveSpeed * Time.deltaTime);
//Player Rotation towards Aimer
aimerVec = GameObject.FindGameObjectWithTag("Aimer");
Vector3 aimPos = new Vector3(aimerVec.transform.position.x, transform.position.y, aimerVec.transform.position.z);
gameObject.transform.LookAt(aimPos);
}
}
Attached to Player, the attack I'm trying to execute...
public class PowerBallAttack : NetworkBehaviour {
public GameObject powerPrefab;
public GameObject aimerVec;
public float powerSpeed = 10f;
public float powerLifetime = 4f;
public float timeStamp;
public float coolDown = 2f;
[Client]
void Test()
{
//PlayerMovement playerMovement = this.GetComponent<PlayerMovement>();
//aimerVec = playerMovement.aimerVec;
aimerVec = GameObject.FindGameObjectWithTag("Aimer");
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
return;
Test();
if (Input.GetAxis("RightTrigger") < 0)
{
if(timeStamp <= Time.time)
{
CmdBallAttackFire(powerLifetime);
timeStamp = Time.time + coolDown;
Debug.Log("Right Trigger Fired");
}
}
}
[Command]
void CmdBallAttackFire(float lifeTime)
{
//Spawn Power GameObject
GameObject power = (GameObject)Instantiate(powerPrefab, transform.TransformPoint(0, 1, 2), transform.rotation);
NetworkServer.Spawn(power);
**->This is the part that isn't working!!!** //Set Power's direction and force, destroy after lifeTime seconds
Vector3 aimDirection = new Vector3(aimerVec.transform.position.x, transform.position.y, aimerVec.transform.position.z);
power.GetComponent<Rigidbody>().AddForce((aimDirection - transform.position).normalized * powerSpeed, ForceMode.Force);
Destroy(power, lifeTime);
}
}
How the Aimer is moved (attached to Aimer gameobject)
public class AimerMovement : MonoBehaviour {
public float aimerMoveSpeed = 10f;
void Update () {
Vector3 aimerMoveDirection = new Vector3(Input.GetAxis("RightStickHorizontal"), 0, Input.GetAxis("RightStickVertical"));
GetComponent<Rigidbody>().MovePosition(transform.position + aimerMoveDirection * Time.deltaTime * aimerMoveSpeed);
}
}
Any help is greatly appreciated!
Comment