Hello.
So I am making a mod which I decide would have a wall around the player. The wall rotates towards the player in normal circumstances and that is easily achievable!
The functions to transform used are:
- entity.transform.Translate(x, y, z) to move an entity
- entity.transform.Rotate(x, y, z) to rotate
After doing the transformation, I call either 'SendNetworkUpdate()' or 'SendNetworkImmediateUpdate(false)' to reflect the changes to the client.
All is good, but it seems that the server 'remembers' the initially spawned position and rotation of the object and just doesn't let go, forcing the player to go back everytime he tries to pass through the place where the object initially was, since apparently the player collides with the object (which it shouldn't).
I recorded a situation where initially you can see the initial state of the High External walls and then they snap to their new position (I did NOT use SendNetworkImmediateUpdate, else they would have immediately rotated). After that I go to their place and try to pass through them, but since they were rotate by 90 degrees, the holes I first try to pass through are actually the walls in 'ghost' mode. I should be able to pass, but can't. I am certainly missing something, but have no idea what.
Here is the youtube video:
Here is the code I use for each entity (C#):
- entityName is the 'prefab' name of the entity
- entityLoc is the Vector3 representation of a point in the world
- SpawnEntity returns the BaseEntity that was spawned
Thank you in advance!Code:BaseEntity spawnedEntity = SpawnEntity(entityName, entityLoc); spawnedEntity.transform.Rotate(0, 90, 0); spawnedEntity.TransformChanged(); spawnedEntity.SendNetworkUpdate();
#Edit 1: It seems that if I do the transformation this way:
the player stops from being pushed anymore. However, I believe this is a 'dirty' way to fix the issue and does not tackle it at its core. I hope this is of help to anyone who can explain what is happening!Code:spawnedEntity.gameObject.SetActive(false); spawnedEntity.transform.LookAt(new Vector3(originX, targetY, originZ)); spawnedEntity.gameObject.SetActive(true);
#Edit 2: I delved a bit as to why this code worked and I believe it might have something to do with colliders, however this feels a bit illogical to me (keep on reading to see why).
Essentially, every object is a 'GameObject' in most simple Unity terms. Stuff is attached to the object, such as rigid bodies and colliders. If you have a GameObject without a collider, you can pass through it and nothing would be registered so something happens, thus colliders, once attached to an object, can be used to detect collision and call whatever methods are appropriate.
Apparently there are 'static' colliders, which are in one place when the object spawns and in Unity's docs it says not to move the object if it has a static collider, since the collider won't move (it seems), thus it might be what is happening here. I will try changing it to Kinetic to see if that fixes it.
Now the illogical part is, if the collider doesn't move, then why does the object still block me in the client when I try to pass through the actual object? It's like the collider moves, but also retains its old position, or so to say, the collider copies itself onto the new position thus we get two colliders (which I'm almost sure is not happening, but it's as an example to show what I mean).
#Edit 3: Ok I found out why it was causing this. I added this code:
orCode:((MeshCollider) spawnedEntity.gameObject.GetComponent(typeof(Collider))).convex = true; spawnedEntity.gameObject.AddComponent(typeof(Rigidbody));
before transformations are applied and they fixed the issue I was having, so it IS the collider that might cause this kind of issue. In short, Rust objects use non-convex Mesh Colliders and have no rigid bodies, thus the colliders are static. SO yeah, like transforming an object with a static collider is a big no-no!Code:spawnedEntity.gameObject.AddComponent(typeof(Rigidbody)); ((Rigidbody)spawnedEntity.gameObject.GetComponent(typeof(Rigidbody))).isKinematic = true;
Solved Transformed entity leaves an unpassable 'ghost'
Discussion in 'Rust Development' started by TeamRequem, Sep 8, 2016.
-
Why move the entity?
Destroy it and re spawn it at the X/Y/Z you want
EDIT:
Are you spawning it and then moving it so it aligns? -
Depends on what you want to achieve. Say you have a box and there's a mini-game like 'catch the box', so it teleports around but would be the same box still (not a real mod, just an example out the top of my head). So translating (moving) it would in my opinion prove much more efficient than destroying it and respawning it, since a lot more goes into that, whereas moving it is just displacing its position and this is quickly mirrored by the renderer, thus a lot of overhead is avoided.
Imagine you have 100 objects or even 1000 and want to move them. Respawning them would be much less efficient than moving them due to the operations that take place.
Regarding aligning, that was not my intention, but rather to know what I can do in case I decide I want to do this, thus I experimented and saw the rotation/positioning issue. Pretty sure you can get object dimensions and use them in your magic code if you want to align them though.
Check out #Edit 2 in the topic, may have some idea about why this is happening.Last edited by a moderator: Sep 8, 2016
