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 specsowl · Aug 18, 2014 at 03:07 PM · swimmingjavascript class

Trouble with swimming in Unity

Hey there. I am working on an FPS with a swimming option. I have it so that I can jump on the surface of the water, then hit a key to dive. After you go underwater everything turns blue, gravity is low, and I can swim freely in all 3 axis. Easy. I also have it when I exit the water level based on the world height, everything is set to normal. My issue lies with the fact that whenever I try and "surface" while still in the water the fog effects for my water flicker on and off and it won't let me go above the surface. I basically want it so that if I swim close to the top, it will send me into surface mode smoothly. Here's my water script.

  var waterLevel : float;
     var isUnderwater: boolean;
     var normalColor: Color;
     var underwaterColor: Color;
     private var chMotor: Player_Controller;
     private var chLook: Mouse_Look;
     private var surface: boolean;
     private var controller: CharacterController;
     private var moveDirection = Vector3.zero;
     
     function Start () 
     {
         chMotor = GetComponent(Player_Controller);
         chLook = GetComponent (Mouse_Look);
         controller = GetComponent(CharacterController);
     
     }
     
     function OnTriggerEnter (other: Collider){
         if (other.gameObject.name == "Surface" && !isUnderwater){
         chMotor.gravity = 0;
         surface = true;
         }
     //I can't get it to work when if(other.gameObject.name == "Surface" && isUnderwater) is the trigger function.
     }
     
     
     function Update () 
     {
      if (surface == true && Input.GetKeyUp (KeyCode.E)){
          isUnderwater = true;
          surface = false;
          
      }
     
     
      if ((transform.position.y < waterLevel) != isUnderwater)
      {
          isUnderwater = transform.position.y < waterLevel;
          if (isUnderwater && !surface) SetUnderwater();
          if (!isUnderwater) SetNormal();
          
      
      }
      
     
     }
     
     function SetNormal()
     {
         RenderSettings.fogColor = normalColor;
         RenderSettings.fogMode = FogMode.Linear;
         RenderSettings.fogStartDistance = 0f;
         RenderSettings.fogEndDistance = 25000f;
         chMotor.gravity = -10000;
         chMotor.speed = 100;
         chMotor.speedStore = 100;
         chMotor.runspeed = 250;
         chLook.Axis = chLook.Axis.MouseX;
     
     }
     
     function SetUnderwater()
     {
         RenderSettings.fogColor = underwaterColor;
         RenderSettings.fogMode = FogMode.Linear;
         RenderSettings.fogStartDistance = 149.26f;
         RenderSettings.fogEndDistance = 1000f;
         
         chMotor.gravity = -1000;
         chMotor.speed = 65;
         chMotor.speedStore = 65;
         chMotor.runspeed = 100;
         chLook.Axis = chLook.Axis.MouseXandY;
     
         
     }
 
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Changing FPSController values via C#? 1 Answer

How can I slow Gravity 2 Answers

Using functions of Inherited classes 3 Answers

Adding multiple colliders to my character controller 1 Answer

how to make a swimable water ?? 0 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