- Home /
[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.
Excuse the noobness, but where and how is the Is$$anonymous$$oving declared in the FPS controller script? Or can it refer to the flag on a separate script?
I'm getting a "does not exist in the current context" error.
Answer by ShawnFeatherly · Apr 09, 2015 at 06:30 PM
If you're okay with trying another method all together, here's what I did to setup an FPS style controller script:
In Unity5 import the asset package Standard Assets/Characters.
Create an instance of RigidBodyFPSController.prefab from that package.
Remove it's child object, "MainCamera"
Import the Google cardboard unitypackage.
Replace the "MainCamera" you removed in step #3 with CardboardMain.prefab
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.
Excuse my noobness, but could you advise on how and where to declare the Is$$anonymous$$oving function?
I've looked at the cardboard controls scripts but I'm really new to scripting code so I'm not sure how to integrate it.
I'm getting : 'Is$$anonymous$$oving' does not exist in the current context.
@Dbaxter04, if you're not interested in Cardboard Controls+ then https://developers.google.com/cardboard/unity/reference has details on how to use "Action OnTrigger".
Great stuff! Works much better than the old FPSInputController code!
@Dbaxter04 is$$anonymous$$oving isn't a function it's a boolean variable which you should define at the top of the class. $$anonymous$$ake it public so that you can trigger it from another script:
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (Rigidbody))]
[RequireComponent(typeof (CapsuleCollider))]
public class RigidbodyFPControllerCardboard : $$anonymous$$onoBehaviour
{
public bool is$$anonymous$$oving = false;
[Serializable]
public class $$anonymous$$ovementSettings
{
etc...
and if you add this at the bottom:
void OnEnable(){
Cardboard.SD$$anonymous$$.OnTrigger += TriggerPulled;
}
void OnDisable(){
Cardboard.SD$$anonymous$$.OnTrigger -= TriggerPulled;
}
void TriggerPulled() {
is$$anonymous$$oving = !is$$anonymous$$oving ;
}
} // closes the class (already there in the original code)
} // closes the namespace (already in the original code)
You'll get to trigger this variable on and off (as per my response to the original question).
@baroquedub, I tried adding in your code, but I get the error that "The name 'Cardboard' does not exist in the current context (CS0103)." Do OnEnable(), OnDisable() and TriggerPulled() go at the bottom of the RigidbodyFirstPersonController.cs file? Thanks.
Not quite at the bottom, they need to be included in the main namespace and part of the class (see the comments in the snippet above). Here;s the full file I'm using: RigidbodyFPControllerCardboard.cs
Thanks for the help! (Sorry for the late reply...)
Answer by baroquedub · Oct 31, 2015 at 12:40 PM
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;
}
}
btw. the answer provided by @$$anonymous$$Featherly works much better on Unity5. Although using OnEnable()/OnDisable() to toggle the walking boolean is definitely the way to go :)