Hello, I’m working on a personal project right now to learn Unity. I started using Unity around 2 months ago, and I’ve been having some issues getting cameras and such working.
The game is intended to be a 3D flight shooter which as of right now uses mouse aim and WASD to move. Local singleplayer works, However, upon adding more clients (or making a server, then joining as a client), things get wonky.
Camera focus for EVERY player jumps to the last player that joined, despite each player not losing any actual control.
Additionally, I had a problem where bullet shots fired by the player only followed the direction the player was facing if they were the host. I did find a way to remedy this by making the shot prefab have a high network send rate, but I feel like this just bogs down the system and was wondering if there was a better way to do it.
Here’s some of the code I have so far, bear in mind I am still very much a novice at this:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class MouseAimCamera : NetworkBehaviour {
public GameObject target;
public float rotateSpeed = 5;
void Start() {
transform.parent = target.transform;
//transform.LookAt (target.transform);
}
void LateUpdate() {
float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis ("Mouse Y") * rotateSpeed;
target.transform.Rotate (-vertical, horizontal, 0);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class ClientCam : NetworkBehaviour {
[SerializeField]
public Camera cam;
public override void OnStartClient() {
// Make sure mouse aim knows we're looking at the local player
cam.GetComponent<MouseAimCamera>().target = gameObject;
// Place camera behind player
Vector3 pos = new Vector3 (transform.position.x+0.1f, transform.position.y + 0.9f, transform.position.z - 4.1f);
Instantiate (cam, pos, transform.rotation);
cam.enabled = true;
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class Done_PlayerController : NetworkBehaviour
{
public float speed;
public float turnrt;
public float maxSpeed;
public GameObject shotPrefab;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Update ()
{
if (!isLocalPlayer) {
// This is not the local player, exit.
return;
} else {
if (Input.GetButton ("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
CmdFire ();
}
}
}
void FixedUpdate ()
{
if (!isLocalPlayer) {
return;
} else {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
if (moveVertical != 0) { // Moving forward
GetComponent<Rigidbody> ().AddRelativeForce (Vector3.forward * (moveVertical * speed));
}
if (moveHorizontal != 0) { // Strafing
GetComponent<Rigidbody> ().AddRelativeForce (Vector3.right * (moveHorizontal * (speed*1.7f)));
}
if (GetComponent<Rigidbody> ().velocity.magnitude > maxSpeed) { // Set speed cap so we dont move too fast
GetComponent<Rigidbody> ().velocity = GetComponent<Rigidbody> ().velocity.normalized * maxSpeed;
}
}
}
[Command]
void CmdFire() {
GameObject shot = (GameObject) Instantiate (shotPrefab, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource> ().Play ();
NetworkServer.Spawn (shot);
Destroy (shot, 3.0f);
}
}
Any help would be greatly appreciated.