• 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
0
Question by Razzik · Aug 10, 2015 at 04:42 AM · movementvrscriptingbasicsaccess

Unity Cardboard SDK Trigger Please Help

I've been trying to work this out for hours, driving myself absolutely crazy because this is so simple yet I can't find the work around. I'm working with the Google Cardboard SDK and I'm just trying to make the character move forward on Trigger Toggle. The issue im having is with the whole private/public thing. I have a RigidBodyFirstPersonController with the GetInput(); Method slightly Modified.

 using UnityStandardAssets.CrossPlatformInput;
 using System.Collections;
 
 
 
 namespace UnityStandardAssets.Characters.FirstPerson
 {
 
     [RequireComponent(typeof (Rigidbody))]
     [RequireComponent(typeof (CapsuleCollider))]
     public class RigidbodyFirstPersonController : MonoBehaviour
     {
         [Serializable]
         public class MovementSettings
         {
             public float ForwardSpeed = 8.0f;   // Speed when walking forward
             public float BackwardSpeed = 4.0f;  // Speed when walking backwards
             public float StrafeSpeed = 4.0f;    // Speed when walking sideways
             public float RunMultiplier = 2.0f;   // Speed when sprinting
             public KeyCode RunKey = KeyCode.LeftShift;
             public float JumpForce = 30f;
             public AnimationCurve SlopeCurveModifier = new AnimationCurve(new Keyframe(-90.0f, 1.0f), new Keyframe(0.0f, 1.0f), new Keyframe(90.0f, 0.0f));
             [HideInInspector] public float CurrentTargetSpeed = 8f;
 
 #if !MOBILE_INPUT
             private bool m_Running;
 #endif
 
             public void UpdateDesiredTargetSpeed(Vector2 input)
 
             {
 
                 if (input == Vector2.zero) return;
                 if (input.x > 0 || input.x < 0)
                 {
                     //strafe
                     CurrentTargetSpeed = StrafeSpeed;
                 }
                 if (input.y < 0)
                 {
                     //backwards
                     CurrentTargetSpeed = BackwardSpeed;
                 }
                 if (input.y > 0)
                 {
                     //forwards
                     //handled last as if strafing and moving forward at the same time forwards speed should take precedence
                     CurrentTargetSpeed = ForwardSpeed;
                 }
 #if !MOBILE_INPUT
                 if (Input.GetKey(RunKey))
                 {
                     CurrentTargetSpeed *= RunMultiplier;
                     m_Running = true;
                 }
                 else
                 {
                     m_Running = false;
                 }
 #endif
             }
 
 #if !MOBILE_INPUT
             public bool Running
             {
                 get { return m_Running; }
             }
 #endif
         }
 
 
         [Serializable]
         public class AdvancedSettings
         {
             public float groundCheckDistance = 0.01f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
             public float stickToGroundHelperDistance = 0.5f; // stops the character
             public float slowDownRate = 20f; // rate at which the controller comes to a stop when there is no input
             public bool airControl; // can the user control the direction that is being moved in the air
         }
 
         public bool MoveTransfer;
         public Camera cam;
         public MovementSettings movementSettings = new MovementSettings();
         public MouseLook mouseLook = new MouseLook();
         public AdvancedSettings advancedSettings = new AdvancedSettings();
 
 
         private bool IsMoving;
         private Rigidbody m_RigidBody;
         private CapsuleCollider m_Capsule;
         private float m_YRotation;
         private Vector3 m_GroundContactNormal;
         private bool m_Jump, m_PreviouslyGrounded, m_Jumping, m_IsGrounded;
 
 
 
         public Vector3 Velocity
         {
             get { return m_RigidBody.velocity; }
         }
 
         public bool Grounded
         {
             get { return m_IsGrounded; }
         }
 
         public bool Jumping
         {
             get { return m_Jumping; }
         }
 
         public bool Running
         {
 
             get
             {
  #if !MOBILE_INPUT
                 return movementSettings.Running;
 #else
                 return false;
 #endif
             }
         }
 
 
         private void Start()
 
         {
 
             m_RigidBody = GetComponent<Rigidbody>();
             m_Capsule = GetComponent<CapsuleCollider>();
             mouseLook.Init (transform, cam.transform);
         }
 
 
         private void Update()
         {
 
             RotateView();
 
             if (CrossPlatformInputManager.GetButtonDown("Jump") && !m_Jump)
             {
                 m_Jump = true;
             }
         }
 
 
         private void FixedUpdate()
         {
             GroundCheck();
             Vector2 input = GetInput();
 
 
 
 
             if ((Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon) && (advancedSettings.airControl || m_IsGrounded))
             {
                 // always move along the camera forward as it is the direction that it being aimed at
                 Vector3 desiredMove = cam.transform.forward*input.y + cam.transform.right*input.x;
                 desiredMove = Vector3.ProjectOnPlane(desiredMove, m_GroundContactNormal).normalized;
 
                 desiredMove.x = desiredMove.x*movementSettings.CurrentTargetSpeed;
                 desiredMove.z = desiredMove.z*movementSettings.CurrentTargetSpeed;
                 desiredMove.y = desiredMove.y*movementSettings.CurrentTargetSpeed;
                 if (m_RigidBody.velocity.sqrMagnitude <
                     (movementSettings.CurrentTargetSpeed*movementSettings.CurrentTargetSpeed))
                 {
                     m_RigidBody.AddForce(desiredMove*SlopeMultiplier(), ForceMode.Impulse);
                 }
             }
 
             if (m_IsGrounded)
             {
                 m_RigidBody.drag = 5f;
 
                 if (m_Jump)
                 {
                     m_RigidBody.drag = 0f;
                     m_RigidBody.velocity = new Vector3(m_RigidBody.velocity.x, 0f, m_RigidBody.velocity.z);
                     m_RigidBody.AddForce(new Vector3(0f, movementSettings.JumpForce, 0f), ForceMode.Impulse);
                     m_Jumping = true;
                 }
 
                 if (!m_Jumping && Mathf.Abs(input.x) < float.Epsilon && Mathf.Abs(input.y) < float.Epsilon && m_RigidBody.velocity.magnitude < 1f)
                 {
                     m_RigidBody.Sleep();
                 }
             }
             else
             {
                 m_RigidBody.drag = 0f;
                 if (m_PreviouslyGrounded && !m_Jumping)
                 {
                     StickToGroundHelper();
                 }
             }
             m_Jump = false;
         }
 
 
         private float SlopeMultiplier()
         {
             float angle = Vector3.Angle(m_GroundContactNormal, Vector3.up);
             return movementSettings.SlopeCurveModifier.Evaluate(angle);
         }
 
 
         private void StickToGroundHelper()
         {
             RaycastHit hitInfo;
             if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo,
                                    ((m_Capsule.height/2f) - m_Capsule.radius) +
                                    advancedSettings.stickToGroundHelperDistance))
             {
                 if (Mathf.Abs(Vector3.Angle(hitInfo.normal, Vector3.up)) < 85f)
                 {
                     m_RigidBody.velocity = Vector3.ProjectOnPlane(m_RigidBody.velocity, hitInfo.normal);
                 }
             }
         }
 
 
 
         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;
         }
 
 
         private void RotateView()
         {
             //avoids the mouse looking if the game is effectively paused
             if (Mathf.Abs(Time.timeScale) < float.Epsilon) return;
 
             // get the rotation before it's changed
             float oldYRotation = transform.eulerAngles.y;
 
             mouseLook.LookRotation (transform, cam.transform);
 
             if (m_IsGrounded || advancedSettings.airControl)
             {
                 // Rotate the rigidbody velocity to match the new direction that the character is looking
                 Quaternion velRotation = Quaternion.AngleAxis(transform.eulerAngles.y - oldYRotation, Vector3.up);
                 m_RigidBody.velocity = velRotation*m_RigidBody.velocity;
             }
         }
 
 
         /// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
         private void GroundCheck()
         {
             m_PreviouslyGrounded = m_IsGrounded;
             RaycastHit hitInfo;
             if (Physics.SphereCast(transform.position, m_Capsule.radius, Vector3.down, out hitInfo,
                                    ((m_Capsule.height/2f) - m_Capsule.radius) + advancedSettings.groundCheckDistance))
             {
                 m_IsGrounded = true;
                 m_GroundContactNormal = hitInfo.normal;
             }
             else
             {
                 m_IsGrounded = false;
                 m_GroundContactNormal = Vector3.up;
             }
             if (!m_PreviouslyGrounded && m_IsGrounded && m_Jumping)
             {
                 m_Jumping = false;
             }
         }
     }
 }


And I have another script that passes the IsMoving Bool to that above script to make him move.

 using UnityEngine;
 using System.Collections;
 using UnityStandardAssets.Characters.FirstPerson;
 
 public class TriggerTest : MonoBehaviour {
 
     public bool IsMoving;
 
     void Start()
 
     {
         GameObject Player = GameObject.Find ("RigidBodyFPSController");
         RigidbodyFirstPersonController PlayerScript = Player.GetComponent <RigidbodyFirstPersonController> ();
         bool IsMoving = PlayerScript.IsMoving;
     
 
     }
 
 
     void Update () {
         
         if (Cardboard.SDK.CardboardTriggered) {
 
             IsMoving = true;
             Debug.Log ("Triggered");  
         }
 }
 }
 


Everything runs and it Triggers but it doesn't seem to update the bool and I have no errors or any idea why its not passing the value. Please help!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by andreapomposelli · Aug 11, 2015 at 03:10 PM

try to put this in fixedupdate :
FixedUpdate(){

          IsMoving = true;  

          Debug.Log ("Triggered");  

}

Comment
Add comment · 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
0

Answer by linus133 · Dec 12, 2015 at 06:51 PM

Hey @Razzik, I found the solution to your problem, just make sure you're updating the right variable in the Update function. You're only changing the variable inside the TriggerTest script, but not the variable in the RigidBodyFPSController script.

 void Update () {
             
             if (Cardboard.SDK.CardboardTriggered) {
                 GameObject Player = GameObject.Find ("RigidBodyFPSController");
                 RigidbodyFirstPersonController PlayerScript = Player.GetComponent <RigidbodyFirstPersonController> ();
                 PlayerScript.IsMoving = !PlayerScript.IsMoving ;
                 Debug.Log ("Triggered");  
             }
         }
     }

Change your Update function to this, it works. Also, note the changes, you'll understand what was wrong. Also, I've made it toggle between moving and not moving by changing IsMoving to NOT IsMoving every time the trigger is detected. Change it back to your old code if you're not satisfied. Cheers!

Comment
Add comment · 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
0

Answer by NGC6543 · Feb 06, 2017 at 03:57 AM

unfortunately the script doesn't work...

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Player Movement 0 Answers

[Maybe a bug?] This code worked perfectly in 5.3.5, now it glitches out only on desktop? 0 Answers

Using the vive controller movement as an analog stick 1 Answer

Game crashes when moving VR Camera 0 Answers

How do I setup fixed movement between predetermined tiles? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges