private void InWater() { var waterHegiht = waterSurface.GetWaterHeight(); if (gameObject.transform.position.y <= waterHegiht) { Vector3 newPos = new Vector3(gameObject.transform.position.x, waterHegiht, gameObject.transform.position.z); gameObject.transform.position = newPos; rb.velocity = Vector3.zero; rb.AddForce(new Vector3(0, 0, 0), ForceMode.Acceleration); Vector3 resetPos = start.transform.position; resetPos.y += 0.5f; gameObject.transform.position = resetPos; time = 0; targetPos = gameObject.transform.position; SetEnemysTarget(); } else if (gameObject.transform.position.y > waterHegiht) { rb.AddForce(new Vector3(0, -4.91f, 0), ForceMode.Acceleration); time = 0; } }
// Update is called once per frame void Update() { var height = waterSurface.GetWaterHeight(); if (transform.position.y >= height) { transform.position = new Vector3(transform.position.x, height, transform.position.z); } step = speed * Time.deltaTime; transform.position = Vector3.MoveTowards(transform.position, targetPosition, step); }