- Home /
The question is answered, right answer was accepted
How do I make my FPS Character swim when in contact with water?
I am having a problem trying to make my FPS Controller swim when in contact with WaterProDaytime. I am using the standard Unity FPS controller. Also when I reach my river, I want my FPS to slow down, but NOT swim. Any ideas? Please help me!!!! Also please give me some sample code on this matter. Thank you!!
Answer by Duke_Hast_Mich · Apr 28, 2017 at 05:54 AM
I just added an ocean sized box collider to my central island, set it to trigger and put this script on the terrain
private void OnTriggerEnter(Collider Other)
{
GameObject DaveyJones = Other.gameObject;
if (DaveyJones.tag == "Player")
{
DaveyJones.GetComponent<Rigidbody>().drag = 5;
DaveyJones.GetComponent<Rigidbody>().useGravity = false;
DaveyJones.GetComponent<Rigidbody>().velocity = new Vector3(0f, -.5f, 0f);
DaveyJones.GetComponent<Animator>().SetBool("IsSwimming", true);
DaveyJones.GetComponent<MouseControls>().isSwimming = true;
}
}
private void OnTriggerExit(Collider Other)
{
GameObject DaveyJones = Other.gameObject;
if (DaveyJones.tag == "Player")
{
DaveyJones.GetComponent<Rigidbody>().drag = 1;
DaveyJones.GetComponent<Rigidbody>().useGravity = true;
DaveyJones.GetComponent<Rigidbody>().velocity = new Vector3(0f, 0f, 0f);
DaveyJones.GetComponent<Animator>().SetBool("IsSwimming", false);
DaveyJones.GetComponent<MouseControls>().isSwimming = false;
}
}
Answer by Arminyazdian · Apr 27, 2017 at 12:53 PM
Hi. Unfortunately im not familiar with that package. But it should have a variable for the speed of the character. If you couldnt find it, you can search for it in the script maybe it is private. After you found it, just check if there is a collision between the character and the specific place by OnCollisionEnter() and then, use GetComponent to change the speed. The script would be like this:
public GameObject myObj;
void OnCollsionEnter(Collision Other){
if(Other.gameObject.tag=="Tag"){
myObj.GetComponent<ScriptName>().variableName...;
}
}
just put the game object which has the fps script in it and put the tag of the place which you want to have collision with in "Tag" and in ScriptName write the name of the fps script name and for variableName put the name of the speed variable. Good luck.
Follow this Question
Related Questions
Modifing Standard Assets FPC to swim 0 Answers
Unity 5, script for a character to be able to swim in water 1 Answer
How to make character swim 0 Answers