Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Collision Issues after Relocating or Instantiating

Discussion in 'FPS.Sample Game' started by devinpquinn, Mar 25, 2019.

  1. devinpquinn

    devinpquinn

    Joined:
    Aug 30, 2018
    Posts:
    8
    Hi,

    Wow, I've been posting a lot of questions on here recently, but I swear only after exhaustive googling and hours upon hours of trying to figure it out. The problem my pathetic self is having THIS time is as follows.

    Objects that are in place when the scene is loaded are collided with correctly by the player. However, any object that I instantiate or move via scripting at runtime has issues colliding with the player. For example, one test object - a simple large cube - works fine when placed in the editor. However, if I wish to instantiate a version of that object, or move that object to a different transform position, the player will have an epileptic seizure when moving over/near it. Basically the player is clipping through the object when they shouldn't. I'm confused as to why this is happening to any object at all that moves or is instantiated at runtime. I've tried every conceivable configuration of collision detection and anything else I could think of. I should also note that this problem only comes up in the build, not in the editor. Any suggestions for how to solve or work around this would absolutely butter my biscuit.

    Thanks,
    Devin
     
  2. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    Butterin' yo Biscuit, Baby!

    With the FPS Sample being an Authoritative server-client setup, only what happen on the SERVER version of the application (not Client, not Editor) matters. CLIENT version's of the app ONLY take user input and send it to the server, and the client UPDATEs it's "view(player pov/fps)" of what is happening on the server (what the players see on their PC.)

    This means the Editor, is an odd man... if not told what to do, aka the "PLAY" button. Avoid this, for anything that needs to be networked. The play button should run scripts and stuff that is not 'disabled if not server', however, it will not display what a client will see, as anything not networked, will become out of sync at the client end.

    Then clients look at the view they have of the server(the game), they then send more commands on how to change that view. The client does collision detection as well, but JUST for local prediction, which the server corrects each update...

    That prediction and client based collision, makes you spazz out like you are talking about, when the client and server disagree on what the player is colliding with. AKA on your client you are seeing a box, and hitting it, but it sounds like the server is not seeing that box in the same place, and so lets you move through it. The disagreement between the client and server, provides the clipping/teleporting effect.. as the server wins the fight, sort of.

    I highly recommend running the editor as a SERVER, and launching the application using the 'project' window. Then using the editor as the server, you can get a better idea of what is being see on the client (since you should have that client also running, connected to the editor) and server, and so can correct any disagreements.

    Once you figure this out, (and I attached a picture of how to launch the editor as a server and one client), then you will want to be sure your prefab you are creating, has the needed scripts from the FPS Sample for a movable object :

    Movable.cs - This disables this prefab's rigidbody on the CLIENT, leaves it enabled on SERVER, so you need a rigidbody to use it. This also then replicates what happens on the server(serialize), to the client(deserialize), for the movement of the prefab. It now(v3.0) also interpolates movement!

    SceneEntity.cs - needed to replicate the object to other clients
    ReplicatedEntity.cs - needed to replicate the object to other clients
    GameObjectEntity.cs - needed to replicate the object to other clients

    This would allow you to update a rigidbody movement from the server, out to each of the clients in the same way that the movablecube does in the sample:
    https://github.com/Unity-Technologi...Prefabs/ReplicatedEntities/MovableCube.prefab

    Something to address:
    When a client joins a server, and the prefab that has a movable.cs on it has stopped moving... it will not update any new client's on where thoes prefabs are until they move again. I disabled my non-moving car's movements to save CPU, but found this out. Then hacked in a cheap trick of moving the car up and down by .001 on the Y, once a second, to send updates on all non-moving movables to newly connected clients. I am sure there is a better way to do this, but FYI.

    Now for things that do not move this often, like doors, or knocking down/over items... Get creative and try to optimize, or just ... use the movable script until it breaks :)

    I think the 'run at server' command may be key for "USE" commands (Thanks to another post here), with server-side colliders, to these less-often events. Using ontriggerenter/exit to set flags, then when we are all up to speed... building an ECS Job to check if anything should be changed. But that's for another post!

    Good Luck!
    Have fun,
    Micah
     

    Attached Files:

    Last edited: Mar 25, 2019
  3. devinpquinn

    devinpquinn

    Joined:
    Aug 30, 2018
    Posts:
    8
    Thanks so much for the detailed response! I followed your advice and realized that the object's position isn't changing on the server. My follow-up question is, what's the proper protocol for moving objects via script (from the client) in such a way that the server recognizes the new position? Thanks again for the absolutely stellar reply!
     
    Last edited: Mar 26, 2019
  4. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    You move them on the server! The client then replicates what happens on the server, so that the clients can see it.
    So just be sure your move scripts work on the server, and not on the client. So that the client object's cannot move, where the server object can (aka, the rigidbody.iskinesmic set to true makes that rigidbody unable to move.)

    Take a look at the moveable.cs, it has a check in it "
    https://github.com/Unity-Technologi...ssets/Scripts/Game/Systems/Movable/Movable.cs

    if(Game.GetGameLoop<ServerGameLoop>() == null) //this says "if the gameloop.server doesnt exist (aka if we are the client) set the rigidbody to be unable to move without being told to do so (the rest of this script.)
    {
    GetComponent<Rigidbody>().isKinematic = true;
    }

    Expanding on this, to enable scripts that only run server-side (aka when the gameloop.server is not NULL (!= null)

    The question then becomes "how do I do X on the server as a client" and thats by moving your character, shooting stuff, then using "runatserver" in the console for use commands, and doing checks to be sure that the use request is ok to execute like "is client in collision with a door? ok open it"

    Cheers
    Micah
     
  5. devinpquinn

    devinpquinn

    Joined:
    Aug 30, 2018
    Posts:
    8
  6. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    YES!
     
  7. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    218
    For some reason when I move the position using " transform.position = startingLocation; " sometimes the object doesn't go to where it should. The startingLocation is where the object is located when the game starts and doesn't change, but when moving the object back to that location (using Movable script) it sometimes puts it in a different location...

    The "Damage Area" field moves correctly as does the trigger box collider, but the model itself (which is a child object of the damage area and box collider) messes up.

    Any idea? My only guess is that it is trying to interpolate movement between two values and doesn't go to exactly where I tell it to go but somewhere in between?

    Even with trying to have it recall " transform.position = startingLocation; " again after 0.1 seconds (multiple times) to see if that will help "reset" where it should be it is still off from its actual starting position.

    Also, the model position is different between different clients. The field and trigger area isn't though. No idea what's going on with this.

    EDIT: Looks like that's the issue. If I keep telling it where it should be over and over it will go to the location it should be on the clients from the server. The problem is if you call it too much and it misses a "pick up" attempt then the model still appears there for other players without actually being a pick up. There's like some fine line between moving it away and moving it back enough so that it syncs between clients and I have no idea what that value is as it seems to change on a lot of factors.
     

    Attached Files:

    Last edited: Apr 9, 2019
  8. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    Yah - I don't think they(fps sample) released movable in 3.0, I think they jacked with it and left it messed up a bit. Since its not used in the example...

    So hopefully, when movable is put back in, a "client joined - true up all movables for that client" will be added.

    As we need the client to join, then have the server confirm all movables to them and their locations (think existing movables in the level from the start, and also anything instantiated as well, which clients would know NOTHING about.)

    But I'll be happy enough with the damn physics added to the FPS Sample, as shown at GDC19. Hello @Unity?
     
  9. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    218
    I've been trying to get Unity to answer any of my 5+ questions... haven't gotten a single peep. Really sad since I have multiple school projects riding on it. Could try to work without it, but the levels were designed for those specific conditions...
     
  10. keeponshading

    keeponshading

    Joined:
    Sep 6, 2018
    Posts:
    937
    I also spent several hours to get movables move.
    They Prefab is registered but i found now way.
    Agressive Mastery is the most active here with golden tips.
    Would be cool when only one of the FPSS team members invest 30 min per week to the forum.
    Would help so much for users like me.
     
    AggressiveMastery and Stexe like this.
  11. Stexe

    Stexe

    Joined:
    Feb 2, 2014
    Posts:
    218
    Yup. I've been trying to contact them on Facebook, Twitter, and by email. Barely any word from them. Even the most basic level of help would be invaluable. I spent 10-12 hours getting the most basic pick ups working (Health Pack / Overheal Pack) and asteroids (flying objects that can hit players that are outside in space) and even then they barely work.
     
    AggressiveMastery likes this.
  12. AggressiveMastery

    AggressiveMastery

    Joined:
    Nov 19, 2018
    Posts:
    206
    Thanks @keeponshading :)

    The FPS Team at unity seems to be dropping the ball, I agree. But, it is free.

    /Little Salty, too
    -Micah
     
    Last edited: Apr 11, 2019
    Stexe likes this.