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.
Related
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
The main problem is to render a player body for every other player in a room. However, the player themself must see only attached hands, not his own body (and he must not see others attached hands). Is there a way to do this?
I tried to use a culling mask on cam, but it doesn't make a lot sense.
Culling mask actually make sense. Mark the body mesh with different layer should hide the body.
You just need to enable / disable it base on isLocalPlayer value.
Alternative way of doing this would be using two set prefebs.
A. One is player controlled
B. One for other player.
Where prefeb A only contains hands. But note that this come with other issue like player's body will not cast shadow as it does not exist.
I think using a culling mask makes perfect sense.
I'd use it in addition to disabling your own player mesh and enabling the attached hands if you have authority (if it's yours).
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.
How do I detect Input.GetMouseButtonUp outside a specific GameObject's area in Unity? Are there any Unity Assets for this, or should we do this programmatically?
It certainly has to be done by programming it, whether there is an asset or not. As it is rather easy to program, I doubt there is one.
What you need to understand is, that Input.GetMouseButtonUp(int button) is an event and entirely decoupled from any "position". The description from the API says "Returns true during the frame the user releases the given mouse button.".
So in the frame a mouse button has been released you want to check if the arrow is "touching" or laying over some GameObject (GO). According to your example you will do nothing when the GO was hit, and else do something.
One way to do that is to use the following method:
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Your GameObject will at least need a collider, maybe a rigidbody too in order to be detected by the ray. I'm not completely sure atm if both are needed.
Additional note: Having an UI element, this method could also be used to set some flag while hovering over it e.g. : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html
I have a character, that has a character controller attached to it I am using transform.position, transform.forward and transform.rotate to move with the 'wasd' keys, that all works fine.
However it will not collide with any sort of colliders, whether is be box colliders or mesh colliders. trigger works on my character, i.e. it can trigger an object to destroy if it passes through, but the colliders that are not set to triggers (like walls), don't block the player, the player just goes through them.
Any ideas?
Note: using C#, unity free version 4.5.1
You Should not use transform for colliding.
Add ridged body and get reference to ridgedbody.
Then use reference.addForce
Guys i figured out the problem, as the previous answer said i should not use transform with character controller, however there's no need to change any colliders, i still kept my character controller on my player, but rather i used controller.SimpleMove to move the character, that fixes the problem and i get to keep the character controller on my object (no need to add extra colliders or rigidbody to the player).
http://docs.unity3d.com/ScriptReference/CharacterController.SimpleMove.html