void FixedUpdate() { //Movement CalculateAndAddForceToPoint(new Vector3(0, 0, 0.22f)); CalculateAndAddForceToPoint(new Vector3(0, 0, -0.18f)); //buoyancy GridPoint point = _elementManager.GridPointFromPosition(transform.position, true); if (point.x >= 0 && point.y >= 0) { float waterHeight = _fluidLayer.HeightField[point.x][point.y]; float height = _elementManager.CurrentTotalHeight[point.x][point.y]; float bottom = _collider.bounds.center.y - _collider.bounds.extents.y + _bottomOffest; float buoyancy = (height - bottom) * _buoyancyMultiplyer * Mathf.Lerp(0, 1, waterHeight / _minimumWaterForBuoyancy); _rigidbody.AddForce( 0, buoyancy, 0); } else { Destroy(gameObject, 1.0f); } }
// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(2)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (_colliderToAddOn.Raycast(ray, out hit, float.PositiveInfinity)) { GridPoint gridPoint = _elementManager.GridPointFromPosition(hit.point, true); Vector3 pos = hit.point; pos.y = _elementManager.CurrentTotalHeight[gridPoint.x][gridPoint.y] + _spawnHeightOffset; var floatingPrefab = _floatingObjectPrefabs.ElementAtOrDefault(Random.Range(0, _floatingObjectPrefabs.Count)); var obj = Instantiate(floatingPrefab.gameObject, pos, Quaternion.identity) as GameObject; var floater = obj.GetComponent <FloatingObject>(); floater.Initialize(_elementManager, _fluidLayer); } } }