I'd like to modify the rotation of the camera object via a gamepad. I have code that works in the editor, but doesn't work on iOS. (However, it looks like it's working but being pushed back on the 2nd frame by the VROne code).
I was able to get this working w/ the Rift, but haven't been able to figure it out with VROne yet. For the Rift I added an "offset" to the rotation that was changed by the GamePad joystick. The offset was calculated into the final rotation that also includes the players look direction.
Any idea what part of the code I'd modify to get this properly working with the VROne sdk?
If you want to use your own GameRotation angles (from the gamepad), the values might be getting overwritten by the integrated HeadTracking. Have you tried disabling the VR Head Tracking Script, in the VROneSDKHead object?
The HeadTracking angles are dealt with in the HeadTrackingIOS class, which calls the native static library. You can try adding your gamepad offset to the Quaternion returned on line 43.
Hope this helps, let us know if it worked!
EDIT: Nevermind, the below code works in the editor, but on the iOS device the camera is still forced ahead. I'll try the other answer.
Cool -- that may work, the above answer. It sounds like what I was looking for.
However, per a suggestion on the Unity forum, I implemented this solution: First I made the VROne Object a child of another empty game object, with the localPosition 0,0,0.
On rotation, I rotate the parent, but first make sure the parent is the same position of the child and then the child localPosition is 0,0,0 - -right now the child is doing the physical movement, and the parent does the rotation. I'm not sure if moving the character controller to the parent would work or make something else funky in the SDK, but this seems to work for now.
if (RightStickX.Value != 0)
{
transform.position = childObject.transform.position;
childObject.transform.localPosition = Vector3(0,0,0);
transform.eulerAngles.y += RightStickX.Value * rotationSpeed * Time.deltaTime;
}
Just in case this would help someone, I was able to solve it in a much simpler way.
The VrOneSDK object is a child of the object that I am rotating with my gamepad.
and then in 'vrHeadTracking.cs' I simply changed
transform.rotation = Quaternion.Inverse(initialRotation) * rot;
to
transform.localRotation = Quaternion.Inverse(initialRotation) * rot;
that way the head tracking is relative to my parent object's rotation.
Related
Im in a bit of a pickle. I am working on trying to make objects that I release from my hand in VR adopt the correct velocity and angularVelocity of the controller so their release movement feels natural. This is a simple matter of tracking the controller angularvelocity and velocity and passing it to the opbject upon release:
rigid.velocity = controller.currentVelocity;
rigid.angularVelocity = controller.currentAngularVelocity;
However, and here comes the kicker, I also have a locomotion system that happens to rotate the CameraRig with regards to the head of the player and after doing so these values are all wrong and shifted with regards to the cameraRig as so:
cameraRig.transform.RotateAround(headsetCamera.transform.position, Vector3.up, -(headsetCamera.transform.eulerAngles.y - lastHeadRot.y));
lastHeadRot = headsetCamera.transform.eulerAngles;
I have tried looking for some answers like this (ref) and this (ref) but none of it has worked. Any suggestions would be most welcome. When using other forms of locomotion the angularvelocity and velocity get set correctly to the objects thrown but not once I have used the rotateAround on the cameraRig.
I solved this finally, so I thought I would add the solution here.
Instead of using this code to throw objects:
_rigid.velocity = controller.currentVelocity;
_rigid.angularVelocity = controller.currentAngularVelocity;
I used:
_rigid.AddForce(controller.currentVelocity);
_rigid.angularVelocity = controller.currentAngularVelocity;
this solved the issue and the objects now fly in the expected direction no matter what the roation of the parent is.
I want to be able to direct my player when he is in the air. So I maded a method called CheckTilt() which is called in FixedUpdate() which is basically checking to see if the tablet has been moved in the x direction. The condition inside of CheckTilt() is based on a bool variable called isGrounded which basically is false when the player is airborne and true otherwise that part works fine. However, I cannot get the player to move based on the accelerometer. Here is my method:
public void checkTilt()
{
if (!isGrounded)
{
float tiltSpeed = 10.0f;
float x = Input.acceleration.x;
Vector2 tilted = new Vector2(x, 0);
myRigidBody.AddForce(tilted * tiltSpeed * Time.deltaTime);
}
}
Other Info: I'm building this for android device, and have tested it with the unity remote.
Fortunately the answer is just that nothing like that works with the Unity remote - you must build to try it.
Re your other question: it's almost impossible to guess the "parity". you have to try it, and if that is not right, just multiply by -1f. (What I do is include a UI button on screen, which, swaps the parity - but really just build twice until you get it!)
"I'm still not getting any movement when moved..."
it's an incredible pain in the ass getting the axes right. YES, on fact, it is 100% a fact that you will need to read all 3 axis, even if a 2D game.
Don't forget that
Apple STUPIDLY USE Z-BACKWARDS orientation ...
because they are utter idiots who have never worked in the game industry.
DOCO
But you will almost certainly have to do this:
You are familiar with using the Canvas / UI system, right? if not tell me.
1) make a canvas and add three Text fields
2) do this
public Text showX;
public Text showY;
public Text showZ;
and then
showX.text = yourAccelerometer.x.ToString("f2");
.. same for the others ..
It seems like a pain but I assure, it's the ONLY WAY to figure out what the hell is going on.
I have an object that, after receiving its respective input, it moves this way:
mov = new Vector3((Input.GetAxis("Horizontal") * vel), 0, 0);
transform.position += mov;
But, I want it to bounce back, once it collides with an object.
I´ve made the procedures already (OnCollisionEnter2D(Collsion2D col){bla bla...}), but I need help with what happens on the collision (bouncing back the object)....
I´ve tried giving the collided object a bouncing material, but it just slows it a bit, my guess is that because of the constant force given by the acceleration.
Greetings.
If you move the object with transform.position what you are doing is basically a "teleport" so it will ignore the bouncing material. If you want it to bounce you have to write the physics code to detect a collision and change the movement or you can do addforce to move the object and it will detect collisions and react automatically.
you are teleporting the object at the current time. instead you should use the Rigidbody.addForce this will add a force in the specified direction thus if you do the opposite direction will "bounce" of the object. Another option would be to create a physics material then not bother with the code.
You are not using materials, right?
See if the content of this post may help you, the OP is using a formula using Raycast and the answer guides him to use the Raycast with Layers Maks:
2D bouncing formula doesn't work properly
There is this one also with fixed angles (like Pong), but it uses material (with values: friction: 0, bounciness: 1):
https://gamedev.stackexchange.com/questions/70294/get-gameobject-to-bounce-of-colliders
But if nothing makes sense and you are going crazy and might want to start from zero, there is this official video tutorial on bouncing and sliding in 2D:
https://unity3d.com/learn/tutorials/modules/beginner/2d/sliding-bouncing-2d
I am trying to move my player by using rigidbody.velocity:
rigidbod.velocity = new Vector2 (Input.GetAxis ("Horizontal") * maxSpeed, rigidbod.velocity.y);
the problem is, this messes up of my explosion code. The character is supposed to be knocked back when near an explosion. I know why it happens; if the player is still, the rigidbody's X velocity would be returned as 0, meaning any outside forces pushing the player along the X axis would counteract this. So when I add the explosion, the player cuts to his new position a few units away. It looks very unnatural and jerky, as he should be pushed back, but his code is telling him to be still unless a key is pressed. I'm posting this to see if there's any way I can re-write this code to be able to move the player while being pushed correctly from outside forces. I heard that AddForce works, but when I used it, my player's velocity constantly increased. He is wither way too fast or way too slow. Any ideas on how I can get this to work? I tried adding rigidbody.velocity.x after where it says 'maxspeed' hoping that it would allow outside force input, and it works, but it messes up the movement code, making him go way too fast. I can't seem to get both the explosions and the movement code to work correctly at the same time. Any help would be greatly appreciated. Thanks.
which is exactly why in the Unity docs they explicitly state:
In most cases you should not modify the velocity directly, as this can
result in unrealistic behaviour.
instead of modifying the velocity directly, you should be using an AddForce(..)
Vector2 force = new Vector2 (Input.GetAxis ("Horizontal") * maxSpeed, 0f);
rigidbody.AddForce(force);
//or if in update:
rigidbody.AddForce(force * Time.deltaTime);
I'm working to let a character push objects around. The problem is that as soon as he touches an object he starts moving it, even when the touch was accidental and the character isn't facing the direction of the object.
What I want to do is get the direction the object when a collision happens, and if the camara is really facing that direction allow the player to move it.
Right now I only managed to get the direction of the object, but I don't know how to compare that with the direction of the camera.
This is what I'm trying now:
void OnCollisionEnter(Collision col) {
float maxOffset = 1f;
if (col.gameObject.name == "Sol") {
// Calculate object direction
Vector3 direction = (col.transform.position - transform.position).normalized;
// Check the offset with the camera rotation (this doesn't work)
Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;
if(offset.x + offset.y + offset.z < maxOffset) {
// Move the object
}
}
You can try to achieve this in several different ways. And it is a bit dependent on how precise you mean facing the box.
You can get events when an object is visible within a certain camera, and when it enters or leaves using the following functions. With these commands from the moment the box gets rendered with that camera (so even if just a edge is visible) your collision will trigger.
OnWillRenderObject, Renderer.isVisible Renderer.OnBecameVisible, OnBecameInvisible
Or you could attempt to calculate whether and objects bounding box falls within the camera's view frustum, there for you could use the following geometry commands
GeometryUtility.CalculateFrustumPlanes, GeometryUtility.TestPlanesAABB
Or if you rather have a very precise facing you could also go for a Physics.Raycast So then you will only trigger the event when the ray is hitting the object.
Hope this helps ya out.
Take a compass obj and align it to ur device sorry object than when you move it you can always know where it points to.
theoretically it should work but maybe your object just moves around because of a bug in your simulator motion engine.