• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Kyth'Pth3hk · Dec 25, 2013 at 09:16 PM · character controllerground detectioncharacter motor

Character motor ground detection problems

I'm using a script to control the player character, who has a rigidbody. I don't want to use the standard character controller because it doesn't work with a rigidbody, and I want forces to interact with the player.

The issue is that I have been unable to find a reliable method to see if the player is on the ground or not.

One approach I've tried is to simply a isGrounded bool that is set to false whenever the player jumps, and is set to true whenever the player collides with anything with the OnCollisionStay method. However, this has poor results. Since isGrounded will be set to be true if anything at all is in contact with the player, the player is able to jump up walls, as long as the player maintains contact with the wall. Additionally, since the isGrounded bool is only set to false when that player jumps, if they player falls of something without jumping, they will be able to jump once in midair.

Another approach I tried is to use raycasting to set the isGrounded bool. I raycasted in Vector3.down direction and the length of slightly more than half of the player's height every frame. When it contacted anything, I set isGrounded to true, and otherwise set it to false. This had poor results as well. Since the ray cast is a 2 dimensional line, if the player is touching the ground, but not directly where the ray cast is, for example if the player is on the edge of a cliff, isGrounded will be false, resulting in the player being unable to jump. Additionally, since they raycast goes slightly below where the player touches the ground, sometimes isGrounded will not immediately be set to false when the player jumps, since they player hasn't been jumped far enough away from the ground yet. This can result on the player jumping again on the next frame, making jumps be higher than they should be. I tried modifying the ray casting approach to include multiple ray casts in different areas on the bottom of the player. However, I cannot have an infinite number of ray casts, so there are still situations where the player wasn't able to jump. It also doesn't solve the problem of multiple jumps.

Another problem I have is that the player cannot jump while in contact with a wall, likely due to the friction between the player and wall. This can be prevented by making the player be a very low friction material. However, doing so would make the player have low friction towards everything, which would cause the player to slide down hills.

Another approach could be to use the standard character controller, but somehow simulate physics with it. This sounds like a very convoluted approach, and I doubt the results would be very good, although I have yet to try it.

So how can I fix these issues? Any advice would be appreciated.

Also, the Unity Answers gods have been smiting me, resulting in me being unable to comment on anything. Because of this, I need to post new answers in order to reply to anything.

Cheers.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by iwaldrop · Dec 25, 2013 at 10:55 PM

Check this page. Not only does Aldo provide some source, a second answer by Setsuki suggests that doing a Capsulecast solved his problem of uneven ground which I think address your standing on the edge of a cliff problem.

As for the problem of jumping against walls, using different PhysicMaterials for the walls and floors should allow jumping while against a wall to 'feel' as you desire it.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Wiki

Answer by Kyth'Pth3hk · Dec 26, 2013 at 05:29 PM

@iwaldrop:

Thank you for the suggestion. When I tried it, it didn't work.

My first approach of using it was to perform a capsule cast starting at the bottom of the player, and ending at 0.1f below the bottom. This resulted in isGrounded being true every frame. It likely returned true every time because the capsule cast started at the very bottom of the player, and therefore intersects with them.

To fix the first approach, I tried was to have the capsule cast start 0.01f below the player, so that it wouldn't come in contact with them. This resulted in the capsule cast always returning true. I am unsure of why, but it may be because the capsule cast may actually begin slightly above the Vector3 inputted for the start of the capsule cast, so as to give the capsule it's domed top.

In order to fix this, I tried making the beginning of the capsule cast further below the player, in order to compensate for the domed top. This resulted in the capsule cast always returning false when on flat terrain. The reason why is likely because the capsule cast doesn't hit the terrain, since when the player is on the flat terrain, the terrain's collider under the player is directly touching the player. Since the capsule cast starts slightly below the player, it misses the terrain.

A solution to this would be to have the capsule cast only return true if the collider it intersects with is not the player. However, since capsule casting only returns a bool, and no information on the collider, I don't know how I could implement this approach. I don't know how the poster in the other topic managed to get it to work. Do you have an suggestions?

TLDR: The capsule cast returns true every time because it intersects with the bottom of the player.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Character Motor doesn't reach max speed 0 Answers

Falling Through an Elevator 3 Answers

How to move the character controller and the moving platform it is standing on. 1 Answer

Adding Multiple Slope Limits to the Character Controller 0 Answers

need help in an RTS game...needed fast....very badly! 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges