Been trying to apply multi player to the survival shooter for the sake of learning multi player.
I got multi player working but I have this camera issue.
My player prefab looks like this
CurrentPlayer
PlayerCamera
PlayerWrapper
Gun
GunBarrelEnd
Player
when game loads the Prefab gets cloned into the scene. All that works fine.
Player Camera uses a camera follow script which uses PlayerWrapper as a target
And Player Wrapper has Player Movement script attached to it.
Hope someone can help me I’m frozen atm
public class CameraFollow : MonoBehaviour {
public Transform target;
public float smoothing = 5f;
Vector3 offset;
void Start(){
offset = transform.position - target.position;
}
void FixedUpdate(){
Vector3 targetCamPos = target.position + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
public class PlayerMovement : MonoBehaviour
{
public float speed = 6f;
Vector3 movement;
Animator anim;
Rigidbody playerRigidbody;
int floorMask;
float camRayLength = 100f;
void Awake(){
floorMask = LayerMask.GetMask ("Floor");
anim = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody> ();
}
void FixedUpdate(){
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
Move (h,v);
Turning ();
Animating (h,v);
}
void Move (float h, float v){ // move player in direction
movement.Set (h,0f,v);
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition (transform.position + movement);
}
void Turning(){
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit floorHit;
if(Physics.Raycast(camRay, out floorHit, camRayLength, floorMask)){
Vector3 playertoMouse = floorHit.point - transform.position;
playertoMouse.y = 0f;
Quaternion newRotation = Quaternion.LookRotation (playertoMouse);
playerRigidbody.MoveRotation (newRotation);
}
}
void Animating (float h, float v){
bool walking = h != 0f || v != 0f;
anim.SetBool ("IsWalking", walking);
}
}
If I leave the prefab in the scene. I can control that player just fine as shown below.
The guy in the black bar is the clone prefab player