Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Tyler S · Jan 23, 2015 at 07:05 AM · movementdirectionvirtualreality

[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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dbaxter04 · Jul 27, 2015 at 11:07 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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:

  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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dbaxter04 · Jul 27, 2015 at 09:27 AM 0
Share

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.

avatar image ShawnFeatherly · Aug 02, 2015 at 08:13 AM 0
Share

@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".

avatar image baroquedub · Oct 31, 2015 at 12:56 AM 0
Share

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).

avatar image youngjh · Nov 23, 2015 at 06:17 PM 0
Share

@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.

avatar image baroquedub youngjh · Nov 23, 2015 at 10:08 PM 0
Share

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

avatar image youngjh baroquedub · Dec 01, 2015 at 03:01 AM 0
Share

Thanks for the help! (Sorry for the late reply...)

avatar image
0

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;
     }
 
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image baroquedub · Oct 31, 2015 at 03:45 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

new XRPlugin VR controller is pivoting in the wrong place 0 Answers

VR UI issue 1 Answer

360 Videos are pixelated!! 4 Answers

Graphics look "wiggly" when moving to far from the origin (VR game) 0 Answers

VR app builds and then finished running on Xcode IOS 14 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges