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 rainshoppa · Apr 21, 2019 at 09:17 AM · multiplayer2d gamescript.unity 2dco-op

Need Help Push and pull script for co-op/multiplayer

Hello guys first i want to introduce my self, i am university student that currently making a game for our final project and i really need you guys to help me understand about unity and programming in unity, if you guys wonder how is my unity skills i really cant talk that much i just started about unity and programming not too long but i can provide some of i already watched and learned.

What i learned so far :

  • The Ultimate Guide to Game Development

  • Complete C# Unity Developer 2D

  • Pull and Push Block - Unity Tutorial

  • Some of Brackeys video on YouTube

What i want to ask this subreddit Unity2D is how to make co-op multiplayer what i want to make is 2D game around 2 - 4 player using Xbox stick, i already did some of coding and demo what i struggle so far is How to make "Pull and Push Block" - What i mean is i want to make it for 2 player to push this box/block/obstacle "IF" it is just 1 player they cannot push or pull it, what i am using the coding is same with Pull and Push Block, if you guys can help me i really appreciate it Thank you for taking your time !

Player Push Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerPush : MonoBehaviour
 {
     public float distance = 1f;
     
     public LayerMask boxMask;
     private PlayerPlatformerController player;
 
     GameObject box;
     // Use this for initialization
     void Start()
     {
      player = GetComponent<PlayerPlatformerController>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (player.isPlayerOne == true )
         {
             Physics2D.queriesStartInColliders = false;
 
             RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance, boxMask);
 
             if (hit.collider != null && hit.collider.gameObject.tag == "Interactables" && Input.GetButtonDown("B ButtonJoy"))
             {
                 box = hit.collider.gameObject;
                 box.GetComponent<FixedJoint2D>().connectedBody = this.GetComponent<Rigidbody2D>();
                 box.GetComponent<FixedJoint2D>().enabled = true;
                 box.GetComponent<BoxPull>().beingPushed = true;
 
             }
             else if (Input.GetButtonUp("B ButtonJoy"))
             {
                 box.GetComponent<FixedJoint2D>().enabled = false;
                 box.GetComponent<BoxPull>().beingPushed = false;
             }
         }
     }
 
     void OnDrawGizmos()
     {
         Gizmos.color = Color.yellow;
         if(PlayerPlatformerController.playerOneDir==1)
         {
             distance = 2f;
             Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.right * transform.localScale.x * distance);
         }
         else if (PlayerPlatformerController.playerOneDir == 0)
         {
             distance = -2f;
             Gizmos.DrawLine(transform.position, (Vector2)transform.position + Vector2.left * transform.localScale.x * distance);
         }
     }
 }

Box Pull Script

 using UnityEngine;
 using System.Collections;
 
 public class BoxPull : MonoBehaviour
 {
     public float defaultMass;
     public float imovableMass;
     public bool beingPushed;
     float xPos;
 
     public Vector3 lastPos;
 
     public int mode;
     public int colliding;
     // Use this for initialization
     void Start()
     {
         xPos = transform.position.x;
         lastPos = transform.position;
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if (mode == 0)
         {
             if (beingPushed == false)
             {
                 transform.position = new Vector3(xPos, transform.position.y);
             }
             else
             {
                 xPos = transform.position.x;
             }
         }
         else if (mode == 1)
         {
 
             if (beingPushed == false)
             {
                 GetComponent<Rigidbody2D>().mass = imovableMass;
             }
             else
             {
                 GetComponent<Rigidbody2D>().mass = defaultMass;
                 //    GetComponent<Rigidbody2D> ().isKinematic = false;
             }
         }
     }
 }


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

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Drop In Drop Out System with Input? 1 Answer

Unity 2D How to Scale or Re-position sprite relative to different Screen sizes 1 Answer

Some tile map images transparency is not working..!!!,Some Tile map images transparency is not working...!!! 0 Answers

How To Make a Snake-like Movement without Food ? [UNITY 2D] 1 Answer

MP - Enter & Exit Any Vehicle 2 Answers


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