- Home /
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 :
- 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;
             }
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                