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…
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class swim : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float height = 0;
height = transform.position.y;
if (height < 41.0) {
this.GetComponent<Rigidbody> ().useGravity = false;
} else {
this.GetComponent<Rigidbody> ().useGravity = true;
}
}
}
using UnityEngine;
using System.Collections;
public class Swimming : MonoBehaviour {
public float thrust;
public float jump;
public Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetKey("w")){
transform.position += transform.forward * Time.deltaTime * thrust;
}
if (Input.GetKey("a")){
transform.position += -transform.right * Time.deltaTime * thrust;
}
if (Input.GetKey("d")){
transform.position += transform.right * Time.deltaTime * thrust;
}
if (Input.GetKey("s")){
Debug.Log ("forwards");
transform.position += -transform.forward * Time.deltaTime * thrust;
}
float height = 0;
height = transform.position.y;
if (height < 41.0) {
if (Input.GetKey("space")){
Debug.Log ("up");
rb.AddRelativeForce(transform.up * jump);
}
if(Input.GetKey(KeyCode.LeftControl)){
Debug.Log ("down");
rb.AddRelativeForce(transform.up * -jump);
}
}
}
}