Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by superluigi · Aug 27, 2013 at 11:26 PM · swimming

Adding multiple colliders to my character controller

So while planning out how to write my swimming script I came across the realization that character controllers don't rotate. Naturally while swimming the character needs to be in a horizontal position. The only solution i have come up with is having a sphere for my characters head inactive and when I enter the water activating it. The head would be located at the midpoint of the character height and in front of my character. I could do the same thing with legs. Also upon entering the water I would shrink my controllers height so that essentially the character is comprised of 3 separate spheres. Is this a logical way of doing this or does anyone know of a better method?

Comment
Add comment · Show 4
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 superluigi · Aug 27, 2013 at 11:31 PM 0
Share

I just tried adding a sphere as a child on top of my character and jumped into a roof. The sphere went through the roof. I guess I wont be able to use a character controller for swim$$anonymous$$g seeing as all not controller colliders will ignore collisions.

avatar image getyour411 · Aug 27, 2013 at 11:38 PM 0
Share

I like this topic as it's something I want to add to my game too eventually. See if this helps http://answers.unity3d.com/questions/31734/allowing-player-to-swim-or-dive-in-water.html

The idea of a swim$$anonymous$$g animation that has the player rotated sounds like it has potential.

avatar image superluigi · Aug 28, 2013 at 04:17 AM 0
Share

yea notice they're all talking about using a rigidbody. I was thinking, what if you only use the character controller collider to interact with the environment but not for the actual hits. I read that you can add colliders to your characters bones. This would probably let you stick most of your character inside walls when underwater, however, getting hit would still work accurately underwater

avatar image getyour411 · Aug 28, 2013 at 08:05 AM 0
Share

I got into the swim$$anonymous$$g part and forgot about the charcontroller part. I think I'll test that by increasing the radius while the player is in the water just enough to extend it up near his head

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by getyour411 · Aug 28, 2013 at 07:57 AM

I wanted to add swimming to my game and I'm also using a character controller (a modified form anyway) so here's what I came up with after reading a bit more about this. For now, it's only 'topside' swimming, no diving down. This is attached to the player:

 using UnityEngine;
 using System.Collections;
 
 public class QM_Swimming : MonoBehaviour {
     
     private Transform myTransform;
     public float waterDepthInitial;        // these are public to play with in Editor
     public float waterDepthCurrent;        // once they are set, change them to private if you prefer
     public float myTreadingDepth;
     public bool goDeeper;                // these two remain public since they are referenced by the 
     public bool inWater;                // CharacterController or equivalent script
 

     void Start () {
         myTransform = transform;
         myTreadingDepth = 1.4f;        //adjust this base on your models height, game scale, etc
         goDeeper = true;
         inWater = false;
         
     }
     
     void OnTriggerEnter(Collider collider) {
         if(collider.CompareTag("SwimWater")) {
             // Grab the Y we entered the water at
             waterDepthInitial = collider.gameObject.transform.position.y;
             waterDepthCurrent = waterDepthInitial;
             inWater=true;
         }
     }
     
     void OnTriggerExit(Collider collider) {
         if(collider.CompareTag("SwimWater")){
             waterDepthInitial = 0f;
             waterDepthCurrent = 0f;
             inWater=false;
         }
     }
     
     //Update is called once per frame
 
     void Update () {
         
         if(inWater) {
             
             // Check how deep we are
             waterDepthCurrent = waterDepthInitial - myTransform.transform.position.y;
         
             // Have we gone to max "wading" depth where we don't want to go deeper/lower on Y
             if(waterDepthCurrent > myTreadingDepth && goDeeper) {
                 goDeeper = false;
             }
             
             // Have we come closer to land/gone up
             if(waterDepthCurrent <= myTreadingDepth && !goDeeper) {
                 goDeeper = true;
             }
             
         }
         
     }
 
 }

To the game world, I added a Simple Water prefab atop a deep depression to ensure I would check how the player would react (i.e. his feet aren't touching the ground). I tagged that "SwimWater" (per the collider call) and added a Mesh Collider with IsTrigger checked.

Lastly, in the Character Controller script there's probably two changes needed:

1) It probably has a whole section that's protected from a isGrounded check, I had to modify that to if(Isgrounded || qmswim.inWater) (where qmswim is a reference back to the QM_Swimming script)

2) It probably has a line like moveDirection.y -= gravity * Time.deltaTime; which I changed to

     if(qmswim.goDeeper) {
 moveDirection.y -= gravity * Time.deltaTime; 
     }

Such that once we hit the depth where we don't want the player to go deeper, we don't apply gravity and the .y stays flat.

And that did it. I'm going to incorporate some animation state changes and splashy sounds based on some of this and see if I can improve on it. I'll definitely have to figure out something else for the 'dive down' part.

Comment
Add comment · Show 2 · 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 superluigi · Aug 28, 2013 at 10:53 PM 0
Share

pretty good. I assume you used the mesh collider on your water because your water has waves.

avatar image getyour411 · Aug 29, 2013 at 12:06 AM 0
Share

I needed something to fire the OnTrigger stuff. Any form of collider that covers that water would be sufficient and perhaps more efficient.

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity4 GetComponent(CharacterMotor) not working 1 Answer

How to import the object from server to unity 2 Answers

Various Script Problems. 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges