Swimmable water script needed
BEFORE YOU ANSWER PLEASE READ THE FOLLOWING I don't want to do any coding so please just give the code to me. The code should either detect if a charter is in a box which defines water and makes it swimmable or the other way around not using y-axis as a determiner.
Answer by BTCallahan · May 07, 2016 at 04:47 PM
@Chase Does
Keep in mind that both the swimmer and the liquid object will need colliders attached and that the liquid collider will need to be a trigger.
public class Swimmer{//attach this to the character
public bool IsInWater;
public float Buoyancy;//I've found that a value of 4 results in perfect buoyancy
RigidBody rigidBod;
void Start(){
rigidBod = GetComponent<RigidBody>();
}
void FixedUpdate(){
if(IsInWater){
rigidBod.AddForce(0f, Buoyancy * -Physics.gravity.y, 0f);
//using swimming controls
}else{
//use normal controls
}
}
}
public class Liquid{//attach this to the liquid
void OnTriggerEnter(Collider other){
Swimmer swimmer = other.gameObject.GetComponent<Swimmer>();
if(swimmer != null){
swimmer.IsInWater = True;
}
}
void OnTriggerExit(Collider other){
Swimmer swimmer = other.gameObject.GetComponent<Swimmer>();
if(swimmer != null){
swimmer.IsInWater = False;
}
}
}
So I attach this to my player? Sorry really afraid of having to restart making my character. public class Swimmer{//attach this to the character
@Chase Does Yeah, that should do it. Sorry about not getting back to you until now.
@BTCallahan how do you attach the script to the player and liquid????
Both classes must inherit from $$anonymous$$onoBehaviour, BTCallahan may have forgotten to add : $$anonymous$$onoBehaviour
when declaring the class
Answer by Frennle · Jan 04, 2017 at 08:57 AM
how do you attach the script to the player and liquid????
You need to go on add component then search new script and then attach your script to this.
when it says: public class Swimmer{//attach this to the character
you need to add :MonoBehaviour to it, like this:`public class Swimmer : MonoBehaviour{` and then drag the script onto your player object
Your answer
![](https://koobas.hobune.stream/wayback/20220612080120im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Any body know how to do water physics? 0 Answers
Low Poly Water with Proper Buoyancy 2 Answers
Unity standard asset water4 with day night cycle? 0 Answers
Does "this." inherit all properties that "gameObject" would? 0 Answers
Weird water problem in Unity 4 0 Answers