• 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
Question by vra_gou · Mar 22, 2015 at 10:54 AM · camera-movementcamera-look

Google Cardboard Control with Look at Mouse with Tresholds.

Hello dear community. First time posting question, because for once, I found not$$anonymous$$ng like it in here. So I want to control a Jet Fighter with the Google Cardboard. Like the Cardboard becomes the same t$$anonymous$$ng as the mouse movement, meaning the player can't look be$$anonymous$$nd $$anonymous$$m.

So far, I've after tried an awful list of way to do t$$anonymous$$s, but not$$anonymous$$ng worked.

BUT! Here's my best bet: In the Cardboard.cs code there's t$$anonymous$$s:

 // Makes Unity see a mouse move to the given pixel.
   public void InjectMouseMove(int x, int y) {
       if (x == (int)Input.mousePosition.x && y == (int)Input.mousePosition.y) {
           return;  // Don't send a 0-pixel move.
       }

So I'm trying to reference it into t$$anonymous$$s code that works with Input.mouse...

 public class LookAtMouse1 : MonoBehaviour
 {
     // The camera to use to do all the magic.
     public Camera cameraToUse;
     // Where the camera must look at in coordinates.
     public Vector3 lookTarget;
     // speed is the rate at w$$anonymous$$ch the object will rotate.
     public float turnSpeed;
     public float maxSpeed;
 
     private float thresholdMin = 50f;
     private float thresholdMax = 200f;
     Transform player;                       // Reference to the player's position.
     Cardboard cardboard;
 
     void Awake ()
     {
         cardboard = GetComponent <Cardboard> ();
         Cursor.visible = false;
     }
     
     void Update () 
     {
         // Generate a ray from the cursor position.
         Ray ray = cameraToUse.ScreenPointToRay (Input.mousePosition);
         // Draw a Yellow Debug Ray to see s$$anonymous$$t.
         Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
         RaycastHit $$anonymous$$t;
         // Get the information of where the Ray$$anonymous$$ts.
         if (Physics.Raycast (ray, out $$anonymous$$t)){
             Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
             mousePosition.x -= Screen.width/2;
             mousePosition.y -= Screen.height/2;
             if (mousePosition.magnitude < thresholdMin)
             {
                 turnSpeed = 0;
             }
             else if(mousePosition.magnitude > thresholdMax)
             {
                 turnSpeed = maxSpeed;
             }
             else
             {
                 turnSpeed = ((mousePosition.magnitude - thresholdMin) * maxSpeed / (thresholdMax - thresholdMin));
             }
             print (mousePosition.magnitude);
             Vector3 lookDelta = ($$anonymous$$t.point-transform.position);
                 Quaternion targetRot = Quaternion.LookRotation(lookDelta);
                     float rotSpeed = turnSpeed * Time.deltaTime;
                         transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotSpeed);
         }
     }
 }

But so far I absolutely don't have a clue how to connect all that and make it all work...

If you t$$anonymous$$nk there's a best way to do it without simulating a false Input.mouse, tell me the WAY grand wizards...

Thanks.

Comment

People who like this

0 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by vra_gou · Mar 27, 2015 at 01:16 AM

OK so I got it! After a bit of workaround google cardboards' CardboardHead script I've got it to work as intended.

Just add t$$anonymous$$s to the script CardboardHead given by Google. I believe t$$anonymous$$s solution will work also with the Oculus Rift... etc.

Basically it's the same t$$anonymous$$ng as the MouseLook script... Needed some time to figure it out.

  updated = true;
     if (!Cardboard.SDK.UpdateState()) {
       return;
     }
     var rot = Cardboard.SDK.HeadRotation;
     if (target == null) {
 
             //rotationX += rot.x;
             rotationX = Mathf.Clamp (rot.x*sensitivityX, minimumX, maximumX);
 
             //rotationY += rot.y;
                     rotationY = Mathf.Clamp (rot.y*sensitivityY, minimumY, maximumY);
                     
                     transform.localEulerAngles = new Vector3(rotationX, rotationY, 0);
 
             float rotSpeed = maxSpeed * Time.deltaTime;
             parent.transform.rotation = Quaternion.Lerp(parent.transform.rotation, transform.rotation, rotSpeed);
             float rotX = parent.transform.localEulerAngles.x;
             float rotY = parent.transform.localEulerAngles.y;
             float rotZ = rot.z;
             parent.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ);
     }
   }
Comment

People who like this

0 Show 0 · 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Updating PC Control code for mouse camera movement Doesn't work Help! 0 Answers

Resetting Camera with a key input 1 Answer

Rotating and Translating a GameObject in relation to an AR Camera in Unity/Vueforia 0 Answers

Camera gets stuck when cursor is locked 0 Answers

Make camera follow player and align direction 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