-
Notifications
You must be signed in to change notification settings - Fork 0
/
float in water.cs
51 lines (40 loc) · 1.74 KB
/
float in water.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Here is a script that will make an object float when touched by the player in an FPS game:
#c#
using UnityEngine;
public class FloatingObject : MonoBehaviour
{
public float floatHeight = 1f; // the height at which the object will float
public float liftForce = 1f; // the force applied to the object to make it float
public float damping = 0.1f; // the rate at which the object's velocity is dampened
private bool isFloating; // whether or not the player is touching the object
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
isFloating = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
isFloating = false;
}
}
private void FixedUpdate()
{
if (isFloating)
{
Rigidbody rb = GetComponent<Rigidbody>();
// calculate the distance between the object and the floatHeight
float distance = transform.position.y - floatHeight;
// apply an upward force proportional to the distance
Vector3 lift = -Physics.gravity * (distance / liftForce);
// dampen the object's velocity to prevent it from bouncing
rb.velocity -= rb.velocity * damping * Time.fixedDeltaTime;
// apply the lift force to the object
rb.AddForce(lift, ForceMode.Acceleration);
}
}
}
#Attach this script to any object that you want to float when touched by the player. Create a new Trigger Collider object that covers the area you want to be considered "touchable". Assign the "Player" tag to this collider object. When the player touches the collider, the object will start floating.