[C#] Using the same input option to start and stop movement

I’m new to this all, but I’m really hyped up for VR and am attempting to make my own basic Google Cardboard apps. I’m currently trying to create movement using the magnetic trigger, and I was successful in doing so (with a lot of google searches and a couple C# versions of the CharacterMotor and FPSInputController scripts). Unfortunately, I can’t seem to use the same trigger to stop the movement.

Here’s the code I’m working with for the FPSInputControllerC:

// Require a character controller to be attached to the same game object
[RequireComponent (typeof (CharacterMotorC))]

//RequireComponent (CharacterMotor)
[AddComponentMenu("Character/FPS Input Controller C")]
//@script AddComponentMenu ("Character/FPS Input Controller")


public class FPSInputControllerC : MonoBehaviour {
	private CharacterMotorC cmotor;
	// Use this for initialization
	void Awake() {
		cmotor = GetComponent<CharacterMotorC>();
	}
	
	// Update is called once per frame
	void Update () {
		// Get the input vector from keyboard or analog stick
		Vector3 directionVector;
		directionVector = Vector3.zero;
		if (Cardboard.SDK.CardboardTriggered) {
			if (directionVector != Vector3.zero) 
			{ 
			directionVector = Vector3.zero;
			}
			else
			{
			directionVector = Vector3.forward;
			}
		
		if (directionVector != Vector3.zero) {
		// Get the length of the directon vector and then normalize it
		// Dividing by the length is cheaper than normalizing when we already have the length anyway
		float directionLength = directionVector.magnitude;
		directionVector = directionVector / directionLength;
			
		// Make sure the length is no bigger than 1
		directionLength = Mathf.Min (1, directionLength);
			
		directionLength = directionLength * directionLength;
			
		directionVector = directionVector * directionLength;
		}
		
		cmotor.inputMoveDirection = transform.rotation * directionVector;
		cmotor.inputJump = Input.GetButton ("Jump");
		}
	}
}

As you can see, I attempted to use a simple if/else statement with the cardboard trigger and the direction of Vector3, but it didn’t do anything. I can still start moving, but I can’t stop.

P.S. If its not too complicated, how do I get the movement to adjust in the direction the camera is facing. So if I started moving forward and turned left, the character would start moving left? If that’s complicated, I’ll just look up some tutorials and maybe come back another day, haha. Just thought I’d ask in case its a simple fix.

If you’re okay with trying another method all together, here’s what I did to setup an FPS style controller script:

  1. In Unity5 import the asset package Standard Assets/Characters.
  2. Create an instance of RigidBodyFPSController.prefab from that package.
  3. Remove it’s child object, “MainCamera”
  4. Import the Google cardboard unitypackage.
  5. Replace the “MainCamera” you removed in step #3 with CardboardMain.prefab
  6. Update or modify a copy of RigidbodyFirstPersonController.cs GetInput() method.

GetInput() with Google Cardboard forward movement fallback:

private Vector2 GetInput()
{
    Vector2 input = new Vector2
    {
        x = Input.GetAxis("Horizontal"),
        y = Input.GetAxis("Vertical")
    };

    // If GetAxis are empty, try alternate input methods.
    if (Math.Abs(input.x) + Math.Abs(input.y) < 2 * float.Epsilon)
    {
        if (IsMoving) //IsMoving is the flag for forward movement. This is the bool that would be toggled by a click of the Google cardboard magnet
        {
            input = new Vector2(0, 1); // go straight forward by setting positive Vertical
        }
    }
    movementSettings.UpdateDesiredTargetSpeed(input);
    return input;
}

I wanted to hold down to move forward. But Google’s SDK only support’s detecting a magnet “click”. If you also want to hold down the magnet to move forward, I recommend using Cardboard Controls+ from the Unity3D Asset Store.

I came across a similar script in Daniel Borowski’s tutorial:
http://danielborowski.com/posts/create-a-virtual-reality-game-for-google-cardboard/

This included a public variable: checkAutoWalk

So I just added a trigger function to toggle it:

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

// Require a character controller to be attached to the same game object
[RequireComponent(typeof(CharacterMotor))]
[AddComponentMenu("Character/FPS Input Controller Horiz")]

public class FPSInputControllerC : MonoBehaviour
{
	private CharacterMotor motor;
	public bool checkAutoWalk = false;


	// Use this for initialization
	void Awake()
	{
		motor = GetComponent<CharacterMotor>();

	}
	
	// Update is called once per frame
	void Update()
	{

		// Get the input vector from keyboard or analog stick
		Vector3 directionVector;
		
		if (!checkAutoWalk) { 
			directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		} else { 
			directionVector = new Vector3(0, 0, 1);
		}

		if (directionVector != Vector3.zero)
		{
			// Get the length of the directon vector and then normalize it
			// Dividing by the length is cheaper than normalizing when we already have the length anyway
			float directionLength = directionVector.magnitude;
			directionVector = directionVector / directionLength;
			
			// Make sure the length is no bigger than 1
			directionLength = Mathf.Min(1.0f, directionLength);
			
			// Make the input vector more sensitive towards the extremes and less sensitive in the middle
			// This makes it easier to control slow speeds when using analog sticks
			directionLength = directionLength * directionLength;
			
			// Multiply the normalized direction vector by the modified length
			directionVector = directionVector * directionLength;
		}
		
		// Apply the direction to the CharacterMotor
		motor.inputMoveDirection = transform.rotation * directionVector;
		motor.inputJump = Input.GetButton("Jump");
	}

	void OnEnable(){
		Cardboard.SDK.OnTrigger += TriggerPulled;
	}
	
	void OnDisable(){
		Cardboard.SDK.OnTrigger -= TriggerPulled;
	}
	void TriggerPulled() {
		checkAutoWalk = !checkAutoWalk;
	}

}