- Home /
Multiplayer Implimentation on 2D Character
Overview:
am trying to make a top-down 2D shooter multiplayer game and I need some indication of how to organize the objects and scripts for my player prefab and camera for the implementation of multiplayer.
What I basically need to know is how convert what I have into multiplayer. (I am quite a Newby). If there are any good tutorials indicating these things that may be good also.
Ignoring my attempts of multiplayer, in singleplayer:
What I have:
I have a character 'Player' with two prefab thingys nested inside of it which are the 'UpperBody' and 'Legs' (I have dragged the legs out from underneath the player).
What I essentially have is an upperbody which rotates to via mouse for shooting and legs which rotate and walk in the direction of the movement force.
The UpperBody:
The 'UpperBody' has ONE script attached which just makes it rotate towards the direction of the mouse.
It contains a head sprite and arms which have a shooting animation in its animator.
(SCRIPT attached to the UPPERBODY which makes it rotate towards mouse)
using UnityEngine; using System.Collections;
public class UpperBody : MonoBehaviour {
void Update () {
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));
}
}
The Legs:
The 'Legs' have ONE script attached which contains one method which sets rotation towards the direction of an input value.
This method is called from the 'PlayerMove' script where I can get direction angle from the getaxisraw.
(Set ROTATION of the LEGS method)
using UnityEngine; using System.Collections;
public class LegsScript : MonoBehaviour {
public void RotateLegs(float A){
transform.eulerAngles = new Vector3 (0, 0, A);
}
}
The Player:
The Player has the 'PlayerMove' script which moves the player (using force)
and activates the animations for shooting the gun and walking the legs. (The walking animation is where the legs (the two ovals) oscillate forwards and back from under the player).
'PlayerMove' also calls the method on the legs to set rotation to the input angle which is determined by getaxisraw.
(To CONTROL the PLAYER)
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour{
public float speed = 16f;
private Rigidbody2D myBody; // RigidBody that I use to move my chracter
[SerializeField]
private Animator anim; // To get the animation for making the legs walk
[SerializeField]
private Animator forgunshootanim; // To get the animation for shooting the gun
[SerializeField]
private LegsScript GetLegs; // To get the method of the legs to rotate them
//--------------------------------------------------------------------------
void Awake()
{
myBody = GetComponent<Rigidbody2D> ();
}
//--------------------------------------------------------------------------
void Update () {
//if (!isLocalPlayer) {return;};
PlayerMoveKeyboard ();
if (Input.GetMouseButtonDown (0)) { // Allows me to do the shoot animation
forgunshootanim.SetBool ("Shoot", true); //
} else { //
forgunshootanim.SetBool ("Shoot", false); //
}
}
//--------------------------------------------------------------------------
void PlayerMoveKeyboard()
{
//Movement
float _xMov = Input.GetAxisRaw("Horizontal");
float _yMov = Input.GetAxisRaw ("Vertical");
Vector2 _movHorizontal = transform.right * _xMov; // (1,0,0)
Vector2 _movVertical = transform.up * _yMov; // (0,0,1)
Vector2 Movement = (_movHorizontal + _movVertical).normalized * speed; // Normalized = 1 unit in direction of addition of vectors.
myBody.AddForce (Movement);
//Animation
if (_xMov != 0 || _yMov != 0) { // If the character is moving
anim.SetBool ("Walking", true); // Do Walking animation
float angle = Vector3.Angle(Movement, transform.up );
// Get the angle of movement (which is incorrect and doesnt do angle right on one side)
float trueangle;
if (_xMov < 0) { // Makes it so that the legs are at the correct angle
trueangle = angle;
} else {
trueangle = 360 - angle;
}
print ("Angle = " + trueangle);
GetLegs.RotateLegs (trueangle); // Calls the script on the Legs
} else {
anim.SetBool ("Walking", false);
// Turns off the walking when the character is not moving
}
}
//--------------------------------------------------------------------------
}
The Camera:
The Camera just has ONE script which makes it find a game object with the 'player' tag and does some sort of smooth transform towards the player.
(SMOOTHLY move camera to PLAYER)
using UnityEngine; using System.Collections;
public class CameaFollow : MonoBehaviour {
private Vector2 velocity;
public float smoothTimeY;
public float smoothTimeX;
private GameObject player;
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
}
void FixedUpdate(){
float posX = Mathf.SmoothDamp (transform.position.x, player.transform.position.x,ref velocity.x,smoothTimeX);
float posY = Mathf.SmoothDamp (transform.position.y, player.transform.position.y,ref velocity.y,smoothTimeY);
transform.position = new Vector3 (posX, posY, transform.position.z);
}
}
What I need:
I guess an exact outline of the changes of structure of the player structure and additions to the scripts including the camera or anything else would be fantastic.
Otherwise a stating roughly what I need or a tutorial would be also good. :D