As the title suggests I am trying to create a character that can both walk and fly throughout the game world. I am trying to figure out the most perfomant way to do this as it will be running on the iphone.
My first attempt was to use a standard character controller and just add some simulated physics that would allow the character to fly. This works for the most part seeing as the character controller is built for walking. However when flying, i didn't have any real control over the rotation of the character (it is very possible i am just missing some method for rotating the character controller). So with this method i could not easily control the character in the air such as flying straight up/down. Also having the character bank in mid-air required an animation being triggered to simulate the character banking. this gave a very awkward look to the whole thing.
My second attempt was with rigidbodies. I have a nice flight script that solves all of my flight problems from above. The problem here is walking. I tried using the rigidbodieFPS script from unify-community in conjunction with a switch script that would handle activating/deactivating either the walker or flyer depending on where the character is. but the character did not look good walking at all (always facing one direction, despite what direction it was actually moving). Once again i could be missing something here that would make it work better.
So my question is if there is a better method to this? is there some magic way to make a character controller and rigidbody play nice with each other? Thanks in advance for any advice or help!
Answer by Bravini
Nov 28, 2010 at 12:54 AM
1 - you could have the objects with the walk and fly script on the same object and turn on/off the righ script with static variables.
2- Instantiate the right script and destroy the other.
What seens to work better for me is the 1st solution, have both scripts on the object and disable the one not correctly being used. While flying, you don't need gravity(set it to 0) or rigidbody functions ,so turn off the rigidbody. While walking, gravity applies normally and you can push objects.
this is pretty much what i am doing now. I tried putting both a rigidbody and a character controller on the player and set the rigidbody to "Is Kinematic" - false when walking and true when flying. I have a script that will enable/disable the appropriate walk/fly script based on if the character is grounded or not. this seems to work pretty well for getting the player to walk effectively, however it seems that the character controller is screwing with my flight script. When flying everything seems real slow and "laggy".
I believe this is the same issue that happens if you try to put a character controller and a rigidbody to a capsule then add the fpswalker.
Try disabling the character controller too and using a movement script instead when flying, or if you can post your scripts ,I can try testing it and give you a better response.
I believe you are right. Problem is that character controller has no "enable/disable" switch like other components. I guess i will have to resort to the hacky destroy/instatiate to get this working.
Answer by The_r0nin
Nov 26, 2010 at 08:26 PM
I had a similar issue myself with multiple modes of travel and multiple meshes. It seems that it would be easier in your case, since you can use the same mesh. Hopefully what I figured out for my game will help you a little...
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
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.
Answers and Comments
No one has followed this question yet.
Character Controller OnControllerColliderHit and Triggers
Collision on character controller (fps player)
Speed up with Rigidbody as frame rate increases c#
Character controller object colliding with a specific ridgidbody