I want to make a complex system of character movement, with such features as wall crawling, double jumps, etc. and with the further possibility of adding complex elements. I have a choice: 1) I tried to do the movement through rigidbody.velocity changing. But I ran into the following problem: I don’t know how to perform the exact BoxAllCast because I don’t know the exact distance that the body will travel with the changed velocity per frame 2) and using rigidbody.MovePosition is bad because, as I understand it, it does not pass in one frame, but I want have full control over the player, and if you make the controller through rigidbody.velocity changing, then you get a lot of stuck in textures, shaking in textures etc.
Please help me decide, maybe someone can share the sources of a complex movement system so that I can see how they implement it, and generally suggest about the best method for changing the position of a rigidbody
Often complex character controllers won't use the Rigidbody component but instead use a Character Controller component as it provides more control over character movement. Physics accurate controllers are difficult to control as they are controlled by physics. Character controllers allow you to define how physics will work on the controller while also being able to 'force' the player to take certain actions (instead of letting the physics engine do so)
As Tony Li says "An accurate physics-based model will feel muddy. Players are used to pressing a direction input and seeing their character move immediately without waiting for forces to propagate through the physics simulation. That's why CharacterController exists."
https://forum.unity.com/threads/how-to-approach-designing-a-complex-character-controller.345923/#post-2238687
Related
I am implementing a player in Unity. The movement of the player is implemented using the character controller. Then, I tried to give a Knock-Back effect, but it is not as effective as using the Rigidbody addForce() function. Is there a way to use a character controller and give an effect like the addForce() function?
I want to implement AddForce() with Character Controller.
One option that is commonly used for rag doll like effects is to temporarily disable the character controller and enable the rigid body. It allows physics to temporarily control the character (and removes the ability of the player to control it). Then, after a set time the character control is re-enabled to regain control.
Welcome to Stackoverflow!
If you desire an effect that can be achieved via a Rigidbody component, it makes no sense to use the Character Controller component, since it is explicitly made for use-cases where you don't want Rigidbody behavior.
To quote the docs for Character Controller :
The Character Controller is mainly used for third-person or first-person player control that does not make use of Rigidbody
physics.
Forthermore, it also illustrates why and when a Character Controller component might be useful:
The traditional Doom-style first person controls are not physically realistic. The character runs 90 miles per hour, comes to a halt immediately and turns on a dime. Because it is so unrealistic, use of Rigidbodies and physics to create this behavior is impractical and will feel wrong. The solution is the specialized Character Controller.
For those reasons, if you actually want Rigidbody behavior, your best option will be to give up on the Character Controller and implement the player behavior with Rigidbodies yourself.
I am currently working on a classical fps game, I want to be able to generate larger numbers "enemies", ideally in the hundreds. Though this may not be feasible. I am hoping to use physics and Rigidbod[ies] for these enemy models.
Obviously the nice solution would be to have mesh colliders on all these models but I'm pretty sure that will affect performance. So my alternative idea was to instead set them up initially with box colliders while they are atleast X distance from the player and then once they enter that radius around the player to switch (via enabling and disabling) these box colliders to mesh colliders.
Is this possible to do and what are the implications? My initial tests caused the model to lose its collision detection with the floor, and so drop through the environment.
Are there are performance implications to switching (enabling/disabling) colliders/if this would even make a difference?
Lastly if none of the above, is there a better solution to this problem that is performant?
The most common practice I know is using multiple primitive colliders under the same rigidbody; I believe this is much more efficient than using mesh colliders, plus, as an added bonus you can even very easily handle special hits (headshots do x2 damage, leg shots slow enemies down) since you know exactly which collider was hit.
Here is an example of how they did it back in the day in Counter Strike
Notice that you will have to place each primitive collider correctly on the bone structure for it to move correctly during animations.
I think you should have two colliders for your ennemies :
1) A persistant box for physical interaction. (This collider would be on a separate layer, colliding only with the physical space.
2) Then, you would have your flexible box / mesh collider. Instead of swapping colliders, enable/disable them. If it is too far, try enabling the box, and if too close, enable the mesh collider. I think enabling and disabling is better for performance than removing and adding components at runtime. Plus, since you already have an 'environmental collider', swapping isn't an issue for the movement of your ennemies.
Hope this helps.
Ok, for this task I need to coordinate physics hitting a game object at a certain point and animation, to create the illusion of punching a character and he stumbles back as if propelled by that contact point.
I have rigid bodies on both the hitting object and character being hit, and can tell when the hitting object enters the character's box collider. What I thought to do first was create an impulse at the contact point then trigger my pre made character animation -
Vector3 direction = (this.transform.position - collider.transform.position) / (this.transform.position - collider.transform.position).magnitude;
this.transform.GetComponent<Rigidbody>().AddForce(direction, ForceMode.Impulse);
Problem is this just makes the character float slowly off opposite the hitting object (Rigidbody has gravity checked on character), and depending on where the character is facing, the animation looks not coordinated with the punch.
I wanted to see whether there's a streamlined way of doing this - how can I create a realistic punch/moving backward situation in Unity?
There's no easy way to get this behavior out-of-the-box with Unity. You will need to script a blending of ragdoll physics with animations.
One approach you might want to try is a system that "pins" your ragdoll to the bones of an animation, and if a collision occurs, the ragdoll unpins itself (partially or completely) from the bones, temporarily. When/if it completes being affected by physics, you would probably want to animate from a keyframe dynamically created based on the position of the ragdoll to a target keyframe.
There are also tools like PuppetMaster on the asset store that are meant to do things like this, but they are often not free, because they are difficult to make well.
I'm trying to build a scene with a number of prefabs placed onto a tiny planet, think something like this. The problem I am facing is that while I can place prefabs on a sphere easily using Control+Shift, they are not rotated and thus appear at terrible rotations. I'm aiming for them to be placed perpendicularly.
Currently I am aware of three solutions:
Place the objects in the scene using Control+Shift then manually rotate them into position.
Place the objects in the scene like before, then add a snippet of code to each of their Update methods: transform.rotation = Quaternion.FromToRotation(transform.up, transform.position - origin) * transform.rotation;
Like option 2, run the code and find some way to save the world state to the scene, but that is easier said than done. It seems like to much effort for something that should be trivial.
The first is tedious and hard to correctly align, the second is easy but renders your scene builder unrepresentative of your final game, and I have no idea where to start on the third. Is there a better way?
In the end I decided to perform all of the orientation manually using Control+Shift. As Johan suggested you could write your own [ExecuteInEditMode] Editor scripts, and if the planet I was foresting was significantly bigger I would have looked into it.
Be aware that if you do venture down the scripting path that the object isn't floating and fully clips through the surface. Manual placement may be necessary after all.
I'm having a problem with Physics! I have imported an object which a bridge from 3ds max to unity 5 ,but the problem is that whenever I want to walk through on it ,I just fall down. It's like there's nothing called BRIDGE! I know there must be a problem about Physics. But how to fix it ?!
When importing a model into Unity it does not have any Collider.
Even though using auto-generated "MeshColliders" is an option
I highly recommend not to use them.
You never need such highly detailed colliders in a game.
Instead, you have to
Add the colliders manually
Here is a little step by step guide
This is the imported model (made with blender) without any collider whatsoever
First thing to do is add a new box collider to the components
as shown in this gif
The next thing to do is scaling the box collider
by either entering the values manually or using the drag/GUI version like I do in the gif below
(you probably know that already but the mode can be changed to orthographic by licking the small cube in between the cones in the upper right)
If necessary you can add as many "detail" collider as you want
by repeating the steps. Sometimes sphere or capsule colliders might fit better but keep in mind that they have a higher resolution!
In this case i added another box collider representing the upper part of the car:
Thats all you need to make things collide with your object
Right now you only have a MeshFilter, MeshRenderer, and Animator component attached. You walk through it because the mesh is only being rendered. In order to add collision, add a MeshCollider component to it.
Joe offers another way to do it with box colliders. There is a bit to know about both methods:
Box colliders will take more time to set up (rather than just adding a
component) and will have less precision. They will have faster performance.
Mesh colliders do not collide well with other mesh colliders.
However, this is often not an issue. If a mesh collider has the
"convex" checkbox checked then it will collide just fine with other
mesh colliders. Small items and any convex-shaped meshes should have
"convex" checked.
For something like your bridge.. it depends on your game. If you are doing a top-down game with limited mobility (no jumping) then box colliders may work for you. If you are making a first-person game then I would strongly suggest mesh colliders. If you decide to use box colliders then you must set them up carefully. Otherwise, physics may not match up with what the player sees!
Do not worry about performance at this time. Worry about it later IF it becomes an issue. With the physics upgrade in Unity 5, it probably won't be.