Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411414 Posts in 69360 Topics- by 58415 Members - Latest Member: sophi_26

April 16, 2024, 08:22:57 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Character Controls and Physics in Unity2D
Pages: [1]
Print
Author Topic: Character Controls and Physics in Unity2D  (Read 1096 times)
Wahtzy
Level 0
*


View Profile WWW
« on: January 20, 2018, 07:53:52 AM »

Heya.

I am designing the workings of the player character for a game I am technical lead for. I am sort of new to the whole thing and it's my first big project in Unity, so the questions might be banal but I'm too much a noob to figure it out my self Smiley

It's a 2D game and we are Using Unity's built in 2D mode, I am also using it's physics engine.
The character needs to be controlled via one of Unity's rigidbodies since physics is a major part of how movement works (you can fall over if you are moving too quick, or if your angle gets too steep). I also want to make him have a soft body so he can be squished by things in the environment.

So far I have sectioned his body into multiple rigidbodies connected with different joints, and then using anima2D made his body sort of follow the underlying parts. This gives me some other problems, since multiple rigidbodies stuck together tend to flip out and distort when too large forces are applied to them which becomes a problem as soon as bouncing platforms or such things are introduced. Also it just looks sort of stiff right now, so my current plan would be to add more rigidbodies and more bones.


(Yes the top collider is too high up, but I was testing things  Shrug)


Gif of how it looks in action:
https://i.imgur.com/NeyMByd.gifv

Another thing is I need to animate his legs, which could easily be done by using anima2d's built in IK, and the animating them using unitys animator, but I would like the legs to be physics enabled too so they could ragdoll when he falls over, or he could hit his knees on things.

I want to know if I'm moving in the right direction and if yes what to do about rigidbodies flipping out. If not what direction should I look to? Another system for softbodies maybe?

Thank you for reading, and if there are any details I missed, you need or you are curios about, don't hesitate to ask  Smiley
Logged
Ordnas
Level 10
*****



View Profile WWW
« Reply #1 on: January 25, 2018, 01:13:37 AM »

I saw the gif and it looks fantastic  Smiley So I think you are on the right path.

About the colliders problem, you could try to check at physics tick time (in FixedUpdate) the distance between the colliders, and if they are too close, offset them a bit, to avoid the flip out by the force impulse. I do not know if that works, but you can give it a try.
Logged

Games:

bateleur
Level 10
*****



View Profile
« Reply #2 on: January 26, 2018, 01:27:56 AM »

The thing to recognise about Unity's physics - both 2D and 3D - is that it's more of an effects system than a way to implement robust game mechanics. Not only will it do freaky things in certain cases, it's also pretty much impossible to define clear limits on what it might do or get any kind of model of expected behaviour.

The solution is conceptually simple, if a bit tedious: if you want some particular constraint to be satisfied, add your own checks independently of the physics system and enforce them in LateUpdate() so that you never draw a frame in which they're violated.

What I'd recommend doing is:

* Sanity checking the relative positions of the character's parts and fixing any radical deformations.
* Capping position or velocity changes each frame based on the previous frame (so to go very fast you must accelerate across multiple frames).
* Giving your character a separate "extreme event" animation that temporarily decouples the animated appearance from the physics model. (You can then interpolate back to it over a few frames.)

Regarding that last suggestion: generally speaking it's a good idea to keep quite a loose coupling between animation and physics, because forcing the physics objects into a particular state is virtually impossible (and not a good idea anyway).
Logged

Wahtzy
Level 0
*


View Profile WWW
« Reply #3 on: February 07, 2018, 05:02:41 PM »

Thank you for your answers!

Okay I think I got it then, it's not really an urgent issue yet, so I have time to experiment yet  Smiley

Am I correct in assuming that I wont have luck with using any physics together with unity's built in animation system? I need to animate the legs, so they can respond with the floor, again because physics and tactility is important, his legs should hit whatever he is walking on and therefore a simple animation wont really work.

My idea so far is to just move the points around to points I raycast for and then move the IK points there. Can unitys animation system take over, like if I wanted to animate the normal walking state with scripting, and then have some animations when you are jumping/falling and stuff like that. I feel like Im missing something obvious but I cant really find any documentation about this.

Anyway thanks!  Smiley
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic