UFPS Tips – Swimming

I’ve updated how I handle UFPS with Swimming. I was recently using Aquas and thought it would be nice to use their effects along with a much simpler transition to swim.

Cons:

  • Simple, as in this does not change the Animator at all so NO Animation changes
  • Swimming is not perfect since it uses Free Fly
  • Sometimes you can just jump out of the water and move real fast, so this should be addressed

Setup: The setup, like I said, is very basic. Add the below script to your Box Collider, add a new state (named Swim) to your UFPS character’s vp_FPController, add the below state preset (named ControllerSwim.txt), and hit play.

SwimInteract Script:

The below script would be placed on a basic object which has only a collider on it. This would be the point where you want the UFPS character to start swimming. For me, I made this just below the surface of the water and extended the box collider far below my terrain just in case. [NOTE: It would be very easy to modify this script to work with triggering other states]

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
///     NOTE: Due to the Weapon Camera forcing only the Weapon Culling Mask, I commented
///     out line 250 of the vp_FPCamera script so that it would allow me to use the 
///     masks that I want (Weapon and Transparent &lt;-Underwater).
/// </summary>

public class SwimInteract : vp_Interactable
{
    public string SwimStateName = "Swim";

    protected override void Start()
    {
        //InteractType == vp_InteractType.Trigger;
        InteractType = vp_InteractType.Trigger;
        base.Start();
    }

    public override bool TryInteract(vp_PlayerEventHandler player)
    {
        if (m_Player == null)
            m_Player = player;

        m_Player.SetState(SwimStateName, true);
        return base.TryInteract(player);
    }

    public void OnTriggerExit(Collider other)
    {
        vp_PlayerEventHandler player = other.transform.root.GetComponent();
        if (player == null)
            return;

        player.SetState(SwimStateName, false);
    }
}

The below is my Swimming Preset State which would be attached to the UFPS Player’s vp_FPController states. I put this at the bottom and named it Swim.

///////////////////////////////////////////////////////////
// Component Preset Script
///////////////////////////////////////////////////////////

ComponentType vp_FPController
MotorAcceleration 0.4
MotorDamping .4
MotorFreeFly True
MotorAirSpeed 1
MotorJumpForce 0.0025 //.0025 good - Amount of up force - default is 0.11
MotorJumpForceDamping .75 //.75 good - How quickly jump force wears off - default is 0.08
MotorJumpForceHold 0.0025 //.0025 good - Jump Held Down Amount of force added per frame - default is 0.003
MotorJumpForceHoldDamping .75 //.75 good - Jump Held Down wear off time - default is 0.5
PhysicsGravityModifier 0.0

Editor Cleanup Script [OPTIONAL]:

If you are like me and you don’t want to have extra stuff showing up in the inspector that you aren’t using, add this script in a folder called Editor (anywhere in your project). Since we are inheriting the vp_Interactable script, it comes with a bunch of things that we really don’t need to change since I kept everything very simple.

Basically I only expose the state name so you can call your state whatever you want.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(SwimInteract))]
public class SwimInteractEditor : Editor
{
    public SwimInteract m_component;

    public void OnEnable()
    {
        m_component = (SwimInteract)target;
    }
    public override void OnInspectorGUI()
    {
        GUILayout.Label("Type the name of the Swimming State you are using when a Player Enters the water.");
        m_component.SwimStateName = EditorGUILayout.TextField("Set Swimming State Name",m_component.SwimStateName);

        if (GUI.changed)
            EditorUtility.SetDirty(target);
    }
}

UFPS Tips – Swimming – OLD WAY BELOW

This tutorial covers setting up Trigger Volumes for a swimming effect.  This effect was created entirely by the VisionPunk team and I do not take credit for it.  I have also created a script to handle the image/camera effects.

The information can be found at the below site.
http://www.opsive.com/assets/UFPS/forum/index.php?p=/discussion/396/swimming-trigger#latest/p1

Water Effect Script

using UnityEngine;
using System.Collections;
using UnityStandardAssets.ImageEffects;//Need this namespace reference to be able to reference the ImageEffects

namespace red
{
public class red_InWater : MonoBehaviour {

//Below are just examples. Other options could be to turn on/off Fog, but I like ImamgeEffects better
public BlurOptimized blurScript=null; //I Put this on the Weapon Camera so the weapon is blurred
public ColorCorrectionCurves colorScript=null; //I Put this on the FPSCamera

//Layer for the Player
public LayerMask playerLayer = 1 &lt;&lt; vp_Layer.LocalPlayer; // layer mask to check for
vp_FPCamera m_camera=null;

void OnTriggerEnter(Collider col)
{
//First check to see if whatever collided is in the layer mask. If not, return
if((playerLayer.value &amp;amp; 1 &lt;&lt; col.gameObject.layer)==0)
return;
m_camera=col.gameObject.GetComponentInChildren&lt;vp_FPCamera&gt;();
TurnOn();
}
void OnTriggerExit(Collider col)
{
//First check to see if whatever collided is in the layer mask. If not, return
if((playerLayer.value &amp;amp; 1 &lt;&lt; col.gameObject.layer)==0)
return;
m_camera=col.gameObject.GetComponentInChildren&lt;vp_FPCamera&gt;();
TurnOff();
}

void TurnOff()
{
//Do your specific stuff here to disable things
blurScript=m_camera.GetComponentInChildren&lt;BlurOptimized&gt;();
blurScript.enabled=false;
colorScript=m_camera.GetComponentInChildren&lt;ColorCorrectionCurves&gt;();
colorScript.enabled=false;
}
void TurnOn()
{
//Do your specific stuff here to enable things
blurScript=m_camera.GetComponentInChildren&lt;BlurOptimized&gt;();
blurScript.enabled=true;
colorScript=m_camera.GetComponentInChildren&lt;ColorCorrectionCurves&gt;();
colorScript.enabled=true;
}
}
}
HeaderWP
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s