Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Swimming and RigidBody Problem! Big Script but need help with one part!

Discussion in 'Scripting' started by Maxske, Aug 4, 2015.

  1. Maxske

    Maxske

    Joined:
    Feb 20, 2015
    Posts:
    37
    When i am underwater, space is to float up, and Q is the float down. I also limit movement speed so it portrays water conditions. The problem is when i float to the surface of the water, if i move a certain way, my characters swims out of the water into the air. So to fix this, i made a variable where if he is floating, it locks his y position so he stays on the surface until the player presses Q to go underwater. The problem is that when i press Q again. The y constraint on the rigid body goes haywire and launches me into the air or it just does nothing. I dont know why this happens! Please help!

    Scroll down to the Floating Method







    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class MovementStamina : MonoBehaviour
    6. {
    7.     CharacterMotor chMotor;
    8.     CharacterController controller;
    9.  
    10.     bool canJump = false;
    11.     float jumpTimer = 1f;
    12.  
    13.     public Image staminaBar;
    14.     GameObject StaminaStamina;
    15.     staminaGUI staminaInfo;
    16.  
    17.     public bool inWater = true;
    18.     underwater water;
    19.     GameObject fps;
    20.     GameObject fps1;
    21.     ConstantForce force;
    22.     MovementStamina movementstamina;
    23.  
    24.     private bool locked = false;
    25.  
    26.    
    27.     // Use this for initialization
    28.     void Start ()
    29.     {
    30.         chMotor = this.GetComponent<CharacterMotor> ();
    31.         controller = this.GetComponent<CharacterController>();
    32.  
    33.         StaminaStamina = GameObject.Find ("StaminaStamina");      
    34.         staminaInfo = StaminaStamina.GetComponent<staminaGUI> ();
    35.  
    36.         fps = GameObject.Find ("Main Camera");
    37.         water = fps.GetComponent<underwater> ();
    38.  
    39.         fps1 = GameObject.Find ("First Person Controller");
    40.  
    41.     }
    42.    
    43.     // Update is called once per frame
    44.     void Update ()
    45.     {
    46.         if(!water.isUnderwater)
    47.         {
    48.             onLand();
    49.         }
    50.  
    51.         if(water.isUnderwater)
    52.         {
    53.             underWater();
    54.         }
    55.  
    56.         if(inWater == true && !water.isUnderwater)
    57.         {
    58.             floating();
    59.         }
    60.  
    61.  
    62.  
    63.  
    64.  
    65.                
    66.  
    67.     }
    68.  
    69.  
    70.     void onLand()
    71.  
    72.     {
    73.         inWater = false;
    74.         RaycastHit hit;  
    75.         Vector3 down = transform.TransformDirection(Vector3.down);
    76.        
    77.         if(Physics.Raycast(transform.position, down, out hit,6))
    78.         {
    79.             if(hit.collider.gameObject.tag == "water")
    80.             {
    81.                
    82.                 inWater = true;
    83.                 chMotor.jumping.enabled = false;
    84.                
    85.             }
    86.            
    87.         }
    88.        
    89.        
    90.         if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
    91.         {
    92.             chMotor.movement.maxForwardSpeed = 10;
    93.             chMotor.movement.maxSidewaysSpeed = 10;
    94.             staminaInfo.staminaBar.fillAmount -= 0.001f;
    95.            
    96.         }
    97.        
    98.         else
    99.         {
    100.             chMotor.movement.maxForwardSpeed = 6;
    101.             chMotor.movement.maxSidewaysSpeed = 6;
    102.         }
    103.        
    104.        
    105.         if (Input.GetKeyDown(KeyCode.Space) && canJump == true)
    106.         {
    107.             staminaInfo.staminaBar.fillAmount -= 0.09f;
    108.             StartCoroutine ("MyMethod");
    109.            
    110.         }
    111.        
    112.         if(canJump == false)
    113.         {
    114.             jumpTimer -= Time.deltaTime;
    115.             chMotor.jumping.enabled = false;
    116.         }
    117.        
    118.         if(jumpTimer <= 0)
    119.         {
    120.            
    121.             canJump = true;
    122.             chMotor.jumping.enabled = true;
    123.             jumpTimer = 1f;
    124.            
    125.         }
    126.         if(staminaInfo.staminaBar.fillAmount < 0.1f)
    127.         {
    128.             canJump = false;
    129.             chMotor.jumping.enabled = false;
    130.             chMotor.movement.maxForwardSpeed = 6;
    131.             chMotor.movement.maxSidewaysSpeed = 6;
    132.         }
    133.     }
    134.  
    135.     void underWater()
    136.  
    137.     {
    138.         inWater = true;
    139.         chMotor.movement.gravity = 2;
    140.         chMotor.movement.maxFallSpeed = 4;
    141.         chMotor.movement.maxForwardSpeed = 4;
    142.         chMotor.movement.maxSidewaysSpeed = 4;
    143.         chMotor.movement.maxBackwardsSpeed = 4;
    144.  
    145.         RaycastHit hit;  
    146.         Vector3 down = transform.TransformDirection(Vector3.down);
    147.         chMotor.jumping.enabled = false;
    148.        
    149.         if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
    150.         {
    151.             chMotor.movement.maxForwardSpeed = 7;
    152.             chMotor.movement.maxSidewaysSpeed = 7;
    153.             staminaInfo.staminaBar.fillAmount -= 0.005f;
    154.         }
    155.  
    156.         if(transform.position.y < 45.6)
    157.         {
    158.             if(Input.GetKey("space"))
    159.             {
    160.                 rigidbody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
    161.             }
    162.         }
    163.  
    164.        
    165.         if(Input.GetKey("q"))
    166.         {
    167.             rigidbody.AddForce(Vector3.down * 5, ForceMode.VelocityChange);
    168.            
    169.             if(Physics.Raycast(transform.position, down, out hit,3))
    170.             {
    171.                 if(hit.collider.gameObject.tag == "ground")
    172.                 {
    173.                     rigidbody.isKinematic = true;
    174.                 }
    175.                
    176.             }
    177.         }
    178.         else
    179.         {
    180.             rigidbody.isKinematic = false;  
    181.         }
    182.  
    183.     }
    184.  
    185.     void floating()
    186.  
    187.     {
    188.         locked = true;
    189.         RaycastHit hit;  
    190.         Vector3 down = transform.TransformDirection(Vector3.down);
    191.         chMotor.jumping.enabled = false;
    192.  
    193.         //chMotor.movement.gravity = 2;
    194.  
    195.  
    196.         if(controller.velocity.magnitude > 1 && Input.GetKey(KeyCode.LeftShift))
    197.         {
    198.             chMotor.movement.maxForwardSpeed = 8;
    199.             chMotor.movement.maxSidewaysSpeed = 8;
    200.             staminaInfo.staminaBar.fillAmount -= 0.005f;
    201.         }
    202.         else
    203.         {
    204.             chMotor.movement.maxFallSpeed = 4;
    205.             chMotor.movement.maxForwardSpeed = 4;
    206.             chMotor.movement.maxSidewaysSpeed = 4;
    207.             chMotor.movement.maxBackwardsSpeed = 4;
    208.         }
    209.  
    210.         if(Input.GetKey("q"))
    211.         {
    212.             locked = false;
    213.             rigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
    214.             transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    215.  
    216.             rigidbody.AddForce(Vector3.down * 5, ForceMode.VelocityChange);
    217.         }
    218.  
    219.        
    220.         if(!locked && transform.position.y > 45.6)
    221.         {
    222.             locked = true;
    223.         }
    224.        
    225.         if(locked)
    226.         {
    227.             rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
    228.             transform.position = new Vector3(transform.position.x, 45.6f, transform.position.z);
    229.            
    230.         }
    231.    
    232.     }
    233.  
    234.     IEnumerator MyMethod() {
    235.  
    236.         yield return new WaitForSeconds(.05f);
    237.         canJump = false;
    238.  
    239.         chMotor.jumping.enabled = false;
    240.     }
    241.    
    242. }