Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by plasmatech8 · Dec 07, 2016 at 09:31 AM · prefabmultiplayerplayer

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.

alt text

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

thegameoverview.png (226.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I have problem with synchronize multiplayer player position... 0 Answers

Multiple player prefabs in the network manager? 4 Answers

Multiplayer prefab player fall through floor on instantiate 0 Answers

How do I get a prefab to know who made it? 1 Answer

Unity Multiplayer - Instantiate prefab 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges