- Home /
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.
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);
}
}
Your answer
Follow this Question
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