Search Unity

C# Swimming Script Problem

Discussion in 'Scripting' started by KingLlama, Aug 2, 2015.

  1. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    Swimming isn't working correctly and I don't understand what I am doing wrong.
    When I jump in the water it's not applying the gravity.....
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4. public class swim : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.         float height = 0;
    14.         height = transform.position.y;
    15.         if (height < 41.0) {
    16.             this.GetComponent<Rigidbody> ().useGravity = false;
    17.         } else {
    18.             this.GetComponent<Rigidbody> ().useGravity = true;
    19.         }
    20.  
    21.  
    22.    
    23.  
    24.     }
    25. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Swimming : MonoBehaviour {
    5.     public float thrust;
    6.     public float jump;
    7.     public Rigidbody rb;
    8.     // Use this for initialization
    9.     void Start () {
    10.         rb = GetComponent<Rigidbody>();
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.         if (Input.GetKey("w")){
    17.  
    18.             transform.position += transform.forward * Time.deltaTime * thrust;
    19.         }
    20.         if (Input.GetKey("a")){
    21.  
    22.             transform.position += -transform.right * Time.deltaTime * thrust;
    23.         }
    24.         if (Input.GetKey("d")){
    25.  
    26.             transform.position += transform.right * Time.deltaTime * thrust;
    27.         }
    28.         if (Input.GetKey("s")){
    29.             Debug.Log ("forwards");
    30.             transform.position += -transform.forward * Time.deltaTime * thrust;
    31.         }
    32.         float height = 0;
    33.         height = transform.position.y;
    34.         if (height < 41.0) {
    35.         if (Input.GetKey("space")){
    36.             Debug.Log ("up");
    37.             rb.AddRelativeForce(transform.up * jump);
    38.         }
    39.             if(Input.GetKey(KeyCode.LeftControl)){
    40.                 Debug.Log ("down");
    41.                 rb.AddRelativeForce(transform.up * -jump);
    42.             }
    43.  
    44.  
    45.  
    46.         }
    47.     }
    48.  
    49.    
    50. }
    51.  
     
  2. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    If you jump into the "water" you should continue to fall because even if you turn off the gravity, you're still moving in that direction and you won't stop until you hit something.. think outer space.

    What you probably want is "when you're in the water" friction is higher so you slow down pretty fast. Then you want to apply a proportional force to the object according to how far under the water the object is.

    if inWater
    add an upward force of (surface - how deep you are) divided by some number n that gives you an appropriate effect

    Without testing ... I think that might give you what you want.

    Alternatively, if you don't want floating to the surface... you can just raise the friction once you hit the water and make sure the forces you're adding to move up and down in the water are strong enough
     
  3. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    The script when working with a scene.....I've gotten this script for this download. Check it out.