- Home /
Network two players collision
Hi, i´m working on network game like agar.io, network works fine with using unet but i have problem ... if i connect two player i spawns same players - Player(Clone) and i have no idea how can i do collision between them. I need if one of them will be bigger then second he can eats him.
Answer by Arshoon · Mar 23, 2016 at 05:01 PM
Collision works the same in multiplayer as it does in single-player. Both player clones will have a Collider on them (most likely a capsule). How you set who is bigger is a problem you will have to tackle. Remember that the actual "eating" should be decided by the server with a Command.
Read this for Colliders: http://docs.unity3d.com/Manual/CollidersOverview.html
This for a sample of a Command: http://unity3d.com/learn/tutorials/topics/multiplayer-networking/adding-multiplayer-shooting?playlist=29690
Yes, but i want copare who is bigger at the moment because they are increasing by eating food. I need distinguish one clone by second clone.
Do you not keep track of what size players are at? It should be trivial.
Use a Command that is sent when players touch, which includes the netId of the players who touched. On the server, figure out who's smaller and then reward the bigger player and kill the smaller player with RPCs.
I have this script, how can i do what you told me please ?
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class AgarScript : NetworkBehaviour {
public Rigidbody2D PlayerRigid;
public float PlusScale;
public float PlusScale1;
public GameObject Player;
//public float speed1;
public Camera myCamera;
public float speed ;
private Vector3 mouselocation;
private Vector3 direction;
//private float movex = 0f;
//private float movey = 0f;
// Use this for initialization
void Start () {
if (!myCamera)
{
myCamera = Camera.main ;
}
}
void Awake() {
Application.targetFrameRate = 300;
}
// Update is called once per frame
void Update () {
if (!isLocalPlayer)
{
return;
}
//$$anonymous$$amaera sleduje hráče
Camera.main.transform.position = new Vector3 (transform.position.x, transform.position.y, Camera.main.transform.position.z);
//Pohyb myší
mouselocation = Input.mousePosition ;
mouselocation.z = 10 ;
direction = myCamera.ScreenToWorldPoint( mouselocation ) ;
transform.position = Vector3.$$anonymous$$oveTowards(transform.position , direction , speed * Time.deltaTime ) ;
////Pohyb šipkama
////var move = new Vector3 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"), 0);
////transform.position += move * speed1 * Time.deltaTime;
//Pohyb šipkama
//movex = Input.GetAxis ("Horizontal");
//movey = Input.GetAxis ("Vertical");
//PlayerRigid.velocity = new Vector2 (movex * speed1, movey * speed1);
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Escape))
Application.Quit();
}
//$$anonymous$$olize
void OnTriggerEnter2D(Collider2D col){
if(col.gameObject.tag == "Food"){
Destroy(col.gameObject);
ScalePlus();
}
//if(col.gameObject.tag == "Hrac"){
// Destroy (col.gameObject);
// ScalePlus1 ();
//}
if(col.gameObject.tag == "Limit"){
print ("asd");
}
}
void ScalePlus(){
gameObject.transform.localScale = new Vector3(transform.localScale.x+PlusScale,transform.localScale.y+PlusScale,transform.localScale.z);
}
void ScalePlus1(){
gameObject.transform.localScale = new Vector3(transform.localScale.x+PlusScale1,transform.localScale.y+PlusScale1,transform.localScale.z);
}
}