Hello Guest

Author Topic: Tutorials on 2D Platformer style controls/physics? So floaty :(  (Read 21230 times)

juts

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Using:

Unity 4x Unity 2D Toolkit from the asset store

My goal is to get a very basic platformer style movement controller set up, but I'm having a very hard time making things not 'floaty.'
Is there a better way to have movement which still responds to collision detection other than using AddForce() on rigid bodies?
Anyone have any tutorials or example projects on the subject?

Thank you

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #1 on: April 27, 2013, 09:44:07 am »
If its floaty it usually means your gravity, drag settings, etc. aren't suited for what you're doing. You should try and tweak them to suit your project first before trying anything more drastic. It certainly is possible to tweak it to do what you want it to.

Alternatively, you can raycast, but that means you'll have to do almost everything yourself.

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #2 on: May 25, 2013, 12:36:36 am »
It seems as though increasing your gravity above some threshold (I'd say around -40 or so) causes the Discrete collision detection to break down; rigidbodies end up overlapping or getting wedged in box colliders. Is there a better way?

A gravity value of around -98.1 (using default tk2d camera settings) seems to be the right value as far as gravity itself is concerned, at least, and it works mostly fine if your collision detection is set to Continuous Dynamic. I'm just not sure if that's the right solution.

I noticed that the Box Stacking demo script uses a regular ol' Orthographic camera w/ size 10. Would it be possible to create an example scene identical to that one but with the tk2d camera instead? For now I've reverted to using the "default" camera that comes with Unity (as in the Box Stacking demo) and it seems to be working OK with a gravity of -9.81.

fsadeq

  • 2D Toolkit
  • Sr. Member
  • *
  • Posts: 353
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #3 on: May 25, 2013, 01:37:23 am »
I recommend using the CharacterController or rolling your own technique instead of using rigidbodies and AddForce()

SullyTheStrange

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 55
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #4 on: May 25, 2013, 02:49:39 am »
I recommend using the CharacterController or rolling your own technique instead of using rigidbodies and AddForce()
From what I've heard this is the way to go. I found a pretty good script from a Youtube video that uses CharacterController, if nothing else you can use it as a base and tweak it for what you need. Download link in the description:

http://www.youtube.com/watch?v=lp4sNXgeiKE

srivello

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #5 on: May 25, 2013, 09:37:03 am »
Hi,

  I'm new to 2DToolkit, but hope to use it as my defacto plugin for all 2D games. I'm using the latest public release of Unity Free and 2D Toolkit as of the time of this post.

It seems in Unity the following can affect the users perception of the speed (i.e. floatiness) of a falling object;

IMPORTANT SETTINGS

Edit-> Project Settings -> Physics;
  • Gravity
Hierarchy->Camera;
  • Perspective (Let's assume Ortho is set)
  • Size (Note: Unity Camera's default is 1 and t2tkCamera's default is 480 in my case)
t2tkSprite (falling because of rigid body component)
  • Mass
  • Scale
  • Drag

By changing any of those values I can affect the perceived falling speed of the sprite. I *think* that no other properties affect the perceived falling speed of the sprite.

CONTROLLED EXPERIMENT

* a. I create a new empty unity scene with a falling sprite. Touching no other settings. Falling looks like a 'good' speed.
* b. I create a new empty unity + 2DToolkit scene with a falling tk2dSprite. Touching no other settings. Falling looks like a 'good' speed.
* c. I open the 2DToolkit demo scene '8- Polygon Colliders' (which uses Unity Camera) Touching no other settings. Falling looks like a 'good' speed.
--
* d. I open the 2DToolkit demo scene '8- Polygon Colliders' (which uses Unity Camera). I add a tk2dCamera and remove the Unity camera. The sprites are not large enough to see well (or at all I forget). I change the camera size and the sprite scale so that I can see.  I change no other settings. Falling speed looks very slow (i.e. floaty).
* e. I create a new empty unity + 2DToolkit scene with a falling tk2dSprite and a tk2dCamera instead of Unity camera. Sprite not visible in 'game'. I change the camera size and the sprite scale so that I can see.  I change no other settings. Falling speed looks very slow (i.e. floaty).

PROBLEM

It seems per a,b,c that default unity settings give you a default (i.e. desirable) physics simulation, and that something in the setup of d,e give floaty results. Floaty results can be 'fixed' by increasing gravity a huge amount. I feel that is a hack workaround that will hurt the accuracy of my overall project and potentially other plugins I may want to use.

Update: I notice that if I set all settings of a tk2dCamera to match a working Unity camera the issues above exist. Then if I remove the 'tk2dCamera' script from the tk2dCamera object, then the game rendering 'works'. So perhaps that script causes the 'problem'.

QUESTIONS

1. Are my assumptions correct in IMPORTANT SETTINGS?
2. Is using tdtkCamera recommended in all projects for 2D Toolkit (I.e. it has no known bugs or issues)? If so, I recommend all demo projects be updated to use it.
3. What is the best way to use 2DToolkit's camera to get the physics results seen in 'c' above (without dramatically changing sprite scale and world gravity?


« Last Edit: May 25, 2013, 09:55:02 am by srivello »

evs

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #6 on: May 25, 2013, 10:53:11 am »
Hello,

I use 2D Platform controller, which uses ray casting for collisions and has ladders ropes moving platforms and more.

It's easy to make a tk2d sprite a child of this controller and these two assets work great together!

https://www.assetstore.unity3d.com/#/content/7381


In the next update supposedly coming next week there is the ability to climb too...

http://www.youtube.com/watch?feature=player_embedded&v=UtWOFyY2lco

http://www.youtube.com/watch?feature=player_embedded&v=YUYiB_Oysy0

$30 in the States (I get stung more being in Europe  :'()


Cheers

evs

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #7 on: May 26, 2013, 12:27:39 am »
It seems as though increasing your gravity above some threshold (I'd say around -40 or so) causes the Discrete collision detection to break down; rigidbodies end up overlapping or getting wedged in box colliders. Is there a better way?

A gravity value of around -98.1 (using default tk2d camera settings) seems to be the right value as far as gravity itself is concerned, at least, and it works mostly fine if your collision detection is set to Continuous Dynamic. I'm just not sure if that's the right solution.

I noticed that the Box Stacking demo script uses a regular ol' Orthographic camera w/ size 10. Would it be possible to create an example scene identical to that one but with the tk2d camera instead? For now I've reverted to using the "default" camera that comes with Unity (as in the Box Stacking demo) and it seems to be working OK with a gravity of -9.81.

Setting up a tk2dCamera scene with Unity physics usually requires the physics settings to be changed. This unfortunately can't be distributed in a unitypacakge or asset store package.

I wrote up a post on how to configure Unity physics to work best with various camera configurations here:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1746.0.html



unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #8 on: May 26, 2013, 12:41:12 am »
Hi,

  I'm new to 2DToolkit, but hope to use it as my defacto plugin for all 2D games. I'm using the latest public release of Unity Free and 2D Toolkit as of the time of this post.

It seems in Unity the following can affect the users perception of the speed (i.e. floatiness) of a falling object;

IMPORTANT SETTINGS

Edit-> Project Settings -> Physics;
  • Gravity
Hierarchy->Camera;
  • Perspective (Let's assume Ortho is set)
  • Size (Note: Unity Camera's default is 1 and t2tkCamera's default is 480 in my case)
t2tkSprite (falling because of rigid body component)
  • Mass
  • Scale
  • Drag

By changing any of those values I can affect the perceived falling speed of the sprite. I *think* that no other properties affect the perceived falling speed of the sprite.

CONTROLLED EXPERIMENT

* a. I create a new empty unity scene with a falling sprite. Touching no other settings. Falling looks like a 'good' speed.
* b. I create a new empty unity + 2DToolkit scene with a falling tk2dSprite. Touching no other settings. Falling looks like a 'good' speed.
* c. I open the 2DToolkit demo scene '8- Polygon Colliders' (which uses Unity Camera) Touching no other settings. Falling looks like a 'good' speed.
--
* d. I open the 2DToolkit demo scene '8- Polygon Colliders' (which uses Unity Camera). I add a tk2dCamera and remove the Unity camera. The sprites are not large enough to see well (or at all I forget). I change the camera size and the sprite scale so that I can see.  I change no other settings. Falling speed looks very slow (i.e. floaty).
* e. I create a new empty unity + 2DToolkit scene with a falling tk2dSprite and a tk2dCamera instead of Unity camera. Sprite not visible in 'game'. I change the camera size and the sprite scale so that I can see.  I change no other settings. Falling speed looks very slow (i.e. floaty).

PROBLEM

It seems per a,b,c that default unity settings give you a default (i.e. desirable) physics simulation, and that something in the setup of d,e give floaty results. Floaty results can be 'fixed' by increasing gravity a huge amount. I feel that is a hack workaround that will hurt the accuracy of my overall project and potentially other plugins I may want to use.

Update: I notice that if I set all settings of a tk2dCamera to match a working Unity camera the issues above exist. Then if I remove the 'tk2dCamera' script from the tk2dCamera object, then the game rendering 'works'. So perhaps that script causes the 'problem'.

QUESTIONS

1. Are my assumptions correct in IMPORTANT SETTINGS?
2. Is using tdtkCamera recommended in all projects for 2D Toolkit (I.e. it has no known bugs or issues)? If so, I recommend all demo projects be updated to use it.
3. What is the best way to use 2DToolkit's camera to get the physics results seen in 'c' above (without dramatically changing sprite scale and world gravity?

The tk2dCamera doesn't cause any "problems" but it does change 1 world unit to be 1 pixel. This means that you will need to change Unity physics to compensate for the difference in scale should you want to use the tk2dCamera.

To answer your questions:
1. The camera doesn't matter, but the physical size of sprites does.

2. There are known issues with the camera preview rendering incorrectly, bug report filed with Unity and they have been fixed in Unity 4.2 as far as I am aware. Its not available to the general public yet.

3. Change the gravity to compensate. It isn't a hack - you're just telling Unity how big your assets are relative to a Unity unit (meter).
If you don't want to change gravity from the default value, don't use the tk2dCamera, use an ortho camera - work out the ideal size by working out the screen height as defined here http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1746.0.html
The ideal ortho size for normal physics will about half that number.

Edit: After the next version of 2D Toolkit (most like 2.1), once we introduce the tk2dOrthoCamera - you will get all the rescaling, aspect ratio compensation goodness with any old orthographic camera.
« Last Edit: May 26, 2013, 12:50:48 am by unikronsoftware »

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #9 on: May 26, 2013, 03:43:00 am »
It seems as though increasing your gravity above some threshold (I'd say around -40 or so) causes the Discrete collision detection to break down; rigidbodies end up overlapping or getting wedged in box colliders. Is there a better way?

A gravity value of around -98.1 (using default tk2d camera settings) seems to be the right value as far as gravity itself is concerned, at least, and it works mostly fine if your collision detection is set to Continuous Dynamic. I'm just not sure if that's the right solution.

I noticed that the Box Stacking demo script uses a regular ol' Orthographic camera w/ size 10. Would it be possible to create an example scene identical to that one but with the tk2d camera instead? For now I've reverted to using the "default" camera that comes with Unity (as in the Box Stacking demo) and it seems to be working OK with a gravity of -9.81.

Setting up a tk2dCamera scene with Unity physics usually requires the physics settings to be changed. This unfortunately can't be distributed in a unitypacakge or asset store package.

I wrote up a post on how to configure Unity physics to work best with various camera configurations here:
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1746.0.html

I switched from the default type of camera to tk2dCamera and then tried changing the gravity to -300. The physics is all jacked up, though. I built a scene that demonstrates the problem: http://bybeardy.com/game/project8/Build.html

The gameobjects are all set up using box colliders built by the sprite collection code. The crates have rigidbodies (mass 1) and box colliders. The player has a character controller and a box collider.

I am a newbie when it comes to Unity, so I could well be doing something horribly wrong, but as far as I can tell increasing the gravity causes tons of problems.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #10 on: May 26, 2013, 09:20:56 am »
Can you send that test scene + script to support at unikronsoftware dot com? If you're using the built in collections, you won't need to include the rest of the project.

srivello

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #11 on: May 26, 2013, 11:35:39 am »
@dpk, your link http://bybeardy.com/game/project8/Build.html is great. It perfectly illustrates the problem that a change to Unity's default gravity is required yet undesirable.

--

@unikronsoftware, If your question is to me, then you can simple add a 'tk2dCamera' to your own example scene '8- Polygon Colliders' and see the problem that a change to Unity's default gravity is required yet undesirable.

Thanks for your answers to me. Per #3, Rather than be required to change Unity's default gravity settings, I'll wait for the release of tk2dOrthoCamera before using 2DToolkit any further. Thanks for info about that.
« Last Edit: May 26, 2013, 11:42:45 am by srivello »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #12 on: May 26, 2013, 12:52:13 pm »
If you add a tk2dCamera to scene 8, then everything will be tiny? How are you scaling it up?

Also - you can use a normal ortho camera - tk2dOrthoCamera is just added on top of that to support scaling for different resolutions.

dpk

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #13 on: May 26, 2013, 04:04:53 pm »
Can you send that test scene + script to support at unikronsoftware dot com? If you're using the built in collections, you won't need to include the rest of the project.

I wasn't using the built in collections (just some of the graphics while learning how to use the collections), so I sent the whole project.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Tutorials on 2D Platformer style controls/physics? So floaty :(
« Reply #14 on: May 26, 2013, 06:47:13 pm »
@dpk - Thanks, I'll investigate it.