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 sovredcat · Sep 12, 2015 at 12:31 PM · networkcollision detectionignorecollision

UNET: How to ignore collisions

Ok so I am instantiating a prefab, which works fine on both the host and all clients. Now my issue is as soon as I add a BoxCollider2D the instantiated prefab will right away collide with the player.

In order to solve that I use Physics2D.IgnoreCollision - however this only works on the host, not on any other clients since it's a Command.

My code is as followed:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;

 public class player_projectile : NetworkBehaviour
 {
     public GameObject projectile;
     public float ProjectileSpeed = 5f;

     GameObject square;

     [Client]
     void Update() 
     {    
         if (Input.GetMouseButtonDown(0))
         {
             GetComponent<AudioSource>().Play ();
             Vector2 direction = (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z +10)) - transform.position).normalized;
         CmdShootProjectile(direction, ProjectileSpeed);
         }
     }

     [Command]
     void CmdShootProjectile(Vector2 direction, float speed)
     {
         square = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
         NetworkServer.Spawn(square);

         Physics2D.IgnoreCollision(gameObject.GetComponent<BoxCollider2D>(), square.GetComponent<BoxCollider2D>());

         square.GetComponent<Rigidbody2D>().velocity = direction * speed;
         Destroy(square, 2);
     }
 }

As you can see I have Physics2D.IgnoreCollision under CmdShootProjectile but this only makes it work for the host. How can I also make it work for every other client connected so they don't collide with their own projectile?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sovredcat · Sep 13, 2015 at 08:26 AM

Ok after googling I have found the solution here

Comment
Add comment · Share
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
avatar image
0

Answer by Erdroy · Sep 12, 2015 at 03:28 PM

You should try to attach a script to the projectile, and IgnoreCollision on Start or FixedUpdate.

Comment
Add comment · Show 4 · Share
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
avatar image sovredcat · Sep 12, 2015 at 03:40 PM 0
Share

I thought about doing that and I could reference the player object in the inspector but I don't want the collision to be ignored on other players just the local player.

So ok, I came up with this:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Networking;

 public class topkek : NetworkBehaviour 
 {
     public Transform localPlayer;

     void Awake () 
     {
         if(isLocalPlayer)
         {
             Physics2D.IgnoreCollision(gameObject.GetComponent<BoxCollider2D>(), localPlayer.GetComponent<BoxCollider2D>());
         }
     }
 }


But it doesn't work at all, collision isn't ignored. How would I go on about doing this?

avatar image Erdroy sovredcat · Sep 12, 2015 at 07:37 PM 0
Share

Hmm...

 // in topkek : NetworkBehaviour
 
 // A small 'proxy' function to call RpcSetNonCollidable
 public void SetNonCollidable(Transform ownerTransform)
 {
       RpcSetNonCollidable(ownerTransform);
 }
 
 [ClientRPC]
 private void RpcSetNonCollidable(Transform ownerTransform)
 {
        Physics2D.IgnoreCollision(GetComponent<BoxCollider2D>(), ownerTransform.gameObject.GetComponent<BoxCollider2D>()); // Ignore the collision
 }
 
 // and in player_projectile / CmdShootProjectile
 square.gameObject.GetComponent<topkek>().SetNonCollidable(transform);


You should make an custom player-id system(ushort synced with SyncVar in eg. PlayerIdentity) to use ins$$anonymous$$d of "Transform ownerTransform". Then

 var players = FindObjectsOfType();  
 foreach (var player in players)
    if(player.playerId == ownerId)
    {
       // Then here ignore the collision here with: player.transform.
       break;
    }



avatar image sovredcat Erdroy · Sep 13, 2015 at 03:34 AM 0
Share

That throws me the error:

UNetWeaver error: Rpc function [topkek:RpcSetNonCollidable] parameter [ownerTransform] is of the type [Transform] which is a Component. You cannot pass a Compent to a Rpc call. Try passing data from within the component.

And I actually already tried that, like so:

 [Command]
 void CmdShootProjectile(Vector2 direction, float speed)
 {
     square = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
     NetworkServer.Spawn(square);

     RpcSetSquareNonCollidable();

     square.GetComponent<Rigidbody2D>().velocity = direction * speed;
     Destroy(square, 2);
 }

 [ClientRpc]
 void RpcSetSquareNonCollidable()
 {
     Physics2D.IgnoreCollision(GetComponent<BoxCollider2D>(), square.GetComponent<BoxCollider2D>());
 }

but I get an error on the client:

NullReferenceException: Object reference not set to an instance of an object player_projectile.RpcSetSquareNonCollidable () (at Assets/scripts/player_projectile.cs:53)

I also tried it with a proxy function like in your example, but it seems no matter what the code only gets executed on the host because of [Command]

I will make something with unique player ID's but I really want to figure this out first. I feel like this should just work, it can't be that hard :/

Show more comments

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

28 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

Related Questions

IgnoreCollision on a Network Instantiated Object 2 Answers

Creating an Authoritative Client and Server 1 Answer

Trigger with object only from top side. 0 Answers

How to make a trigger box collider SOLID (with physics)? 1 Answer

Networking: multiple childs issue 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