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
0
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 nothing like it in here. So I want to control a Jet Fighter with the Google Cardboard. Like the Cardboard becomes the same thing as the mouse movement, meaning the player can't look behind him.

So far, I've after tried an awful list of way to do this, but nothing worked.

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

 // 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 this 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 which 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 shit.
         Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
         RaycastHit hit;
         // Get the information of where the Rayhits.
         if (Physics.Raycast (ray, out hit)){
             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 = (hit.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 think there's a best way to do it without simulating a false Input.mouse, tell me the WAY grand wizards...

Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 this to the script CardboardHead given by Google. I believe this solution will work also with the Oculus Rift... etc.

Basically it's the same thing 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
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Mouse Look Help 2 Answers

How to make camera focus on a game Object in play mode? 1 Answer

My camera is not linking with my movement 0 Answers

Camera to move when i move to the next room 1 Answer

Can someone help me move my camera up a bit? 1 Answer


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