forward movement in camera direction (not direction of rigid body only)

Hi,

before starting It's important that we all know im noobish. Started using unity a few weeks ago for a project on my IMDesign degree and have little coding knowledge.

Ok so my problem is with SWIMMING in my water section. I have a rock type shore with a cave section under the water level. I would like for the user to be able to swim through the cave system as opposed to walking through it.

I am using the standard first person controller

After reading around these answers, videos etc, I am at the stage where I have:

Zero gravity under water by way of a box collider with the script found here attached.

This means that I float on the surface which is great. But I am unable to dive/swim down into the water!

this link allowed me to get as far as I have. I am stumped trying to code forwards movement in the absolute direction of camera

I thought the simplest way would be to grab the underwaterLevel position from this script

//This script enables underwater effects. Attach to main camera.

//Define variables
var underwaterLevel = 7;

//The scene's default fog settings
private var defaultFog = RenderSettings.fog;
private var defaultFogColor = RenderSettings.fogColor;
private var defaultFogDensity = RenderSettings.fogDensity;
private var defaultSkybox = RenderSettings.skybox;
var noSkybox : Material;

function Start () {
    //Set the background color
    camera.backgroundColor = Color (0, 0.4, 0.7, 1);
}

function Update () {
    if (transform.position.y < underwaterLevel) {
        RenderSettings.fog = true;
        RenderSettings.fogColor = Color (0, 0, 0, 0);
        RenderSettings.fogDensity = 0.02;
        RenderSettings.skybox = noSkybox;

    }

    else {
        RenderSettings.fog = defaultFog;
        RenderSettings.fogColor = defaultFogColor;
        RenderSettings.fogDensity = defaultFogDensity;
        RenderSettings.skybox = defaultSkybox;
    }
}

to determine when to switch the movement type? or grab something from the box collider that switches gravity off? Even still, I can't find the exact part of FPS motor script to modify..

Any help would be greatly appreciated as I'm totally confused :|

Sorry for the ramblings - i tend to do that.

Much thanks for reading,

Oli

The forward direction of a transform can be found by

var theForwardDirection = transform.TransformDirection (Vector3.forward);

So finding the forward direction of the main camera isn't hard

var theForwardDirection = Camera.main.transform.TransformDirection (Vector3.forward);

Then of-course this direction will usually be a bit tilted downwards or upwards so you might want to zero that

theForwardDirection.y = 0;

And perhaps normalize it to get a constant speed if you are using it for that.

theForwardDirection.Normalize ();

[Edit] The code in FPSInputController which should be changed:

//Previous code
//var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

//New code
//Forward direction also has a shortcut, transform.forward
var directionVector = Camera.main.transform.forward;
//No downward movement
directionVector.y = 0;
//Scale it by how fast the user wants to move
directionVector *= Input.GetAxis("Vertical");

My suggestion is:

Make your own tiny script.

  1. Let it care about detection for underwater (if you hasn't changing gravity and water level is constant along the scene, then just one float variable and transform.position.y is enough for detection, if not - BoxCollider triggers are for you).
  2. Let it disable all movement control scripts on prefab (use gameObject.GetComponent() for that), but don't disable mouselook!
  3. Take Input data yourself in tiny script and simply convert it. There are few code to help you orient in what you need (this is not actually working code, you should write it yourself in any way you like to do it):

var moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveDirection = Camera.main.transform.TransformDirection(moveDirection);

var controller : CharacterController = GetComponent(CharacterController);
controller.Move(moveDirection * Time.deltaTime);

If you are in dive/swim mode you could try to disable the first person controller and/or switch off physics for your character (isKinematic = true) and manipulate transform.position and transform.rotation.

Here is another useful link: Allowing player to swim or dive in water