How to reset unity accelerometer on Z? - unity3d

someone can tell me how can I reset my accelerometer.
I have a 3D environment, and my ball bounce left-right correctly, my Y is ok, I don't need it, but my z is not ok, I use my device in landscape mode.
Pratically when my devide is 90° (landscape mode) with the floor is in neutral z position.
I need to have neutral position when user start game, I see other answer on unity blog with they don't work for me.
This is my code :
public float posZ;
// Use this for initialization
void Start () {
Salvataggio.control.Load ();
posZ = Input.acceleration.z;
}
void Update () {
trovaPallina = GameObject.FindGameObjectWithTag ("Pallina");
if (trovaPallina) {
trovaPallina.transform.position += new Vector3(Input.acceleration.x / 10, 0, (Input.acceleration.z - posZ) / 10) ;
}
}

Why not create some sort of calibration for it? So, you could create a calibration menu in which you click a button once you are satisfied with the zero position of the device. Store the x, y & z values as your zero values. That way, you can use the current values against your zero values to determine the offset in each direction.

Related

how do I get mouse world position. X Y plane only in unity

how do I get mouse world position. X Y plane only in unity . ScreenToWorldPosition isn't working. I think I need to cast a ray to mouse but not sure.
This is what I am using. doesnt seem to give the correct coordinates or right plane. need for targeting and raycasting.
private void Get3dMousePoint()
{
var screenPosition = Input.mousePosition;
screenPosition.z = 1;
worldPosition = mainCamera.ScreenToWorldPoint(screenPosition);
worldPosition.z = 0;
}
Just need XY coords.
I tried with ScreenToWorldPoint () and it works.
The key I think is in understanding the z coordinate of the position.
Geometrically, in 3D space we need 3 coordinates to define a point. With only 2 coordinates we have a straight line with variable z parameter. To obtain a point from that line, we must choose at what distance (i.e. set z) we want the point sought to be.
Obviously, since the camera is perspective, the coordinates you have at z = 1 are different from those at z = 100, differently from the 2D plane.
If you can figure out how far away, that is, to set the z correctly, you can find the point you want.
Just remember that the z must be greater than the minimum rendering distance of the chamber. I set that very value in the script.
Also remember that the resulting vector will have the z equal to the z position of the camera + the z value of the vector used in ScreenToWorldPoint.
void Get3dMousePoint()
{
Vector3 worldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
print(worldPosition);
}
if you think my answer helped you, you can mark it as accepted and vote positively. I would very much appreciate it :)

Unity input gyro get unbiased angle

I have a game in landscape left mode. What I am trying to do is get the phone tilt on the x and y axis but a bunch of weird things happen. For example, turning the phone on the z axis affects the calculations of the x and z axis. Quaternions are a hard thing to wrap my head around, any help? The code is my closest solution.
public float rotationX;
public float rotationZ;
public float gyroscopeX, gyroscopeY, gyroscopeZ;
public Quaternion rotated;
private void Start ()
{
Input.gyro.enabled = true;
}
private void FixedUpdate ()
{
rotated = ConvertRotation(Input.gyro.attitude) * GetRotFix() * Quaternion.Euler(Vector3.forward * 90);
rotated *= Quaternion.AngleAxis(-Input.gyro.attitude.z, Vector3.forward);
rotationX = ClampAngle(rotated.x*90, -45, 45);
rotationZ = ClampAngle(rotated.y*90, -45, 45);
transform.rotation = Quaternion.Euler(-rotationZ, 0, rotationX);
}
private static Quaternion ConvertRotation(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
private Quaternion GetRotFix()
{
if (Screen.orientation == ScreenOrientation.Portrait)
return Quaternion.identity;
if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.Landscape)
return Quaternion.Euler(0, 0, -90);
if (Screen.orientation == ScreenOrientation.LandscapeRight)
return Quaternion.Euler(0, 0, 90);
if (Screen.orientation == ScreenOrientation.PortraitUpsideDown)
return Quaternion.Euler(0, 0, 180);
return Quaternion.identity;
}
I would like the object to rotate only on its x and z axes according to phone's x and y axes independently. However, turning the phone on its z axis will cause the game object to rotate diagonally if the phone is tilting only on one axis.
In the real world, physically, the phone has an orientation, which is a four dimensional vector: 3 dimensions to describe a forward direction (F), and a 4th dimension to describe how much the phone is rotated (R) about that forward direction.
However, due to hardware limitations and poor planning on the part of platform developers, phones only report Euler angles. That is not sufficient for determining an orientation. What this means is that for each possible screen orientation (i.e. ScreenOrientation.LandscapeLeft you observe, there are at least two (indeed infinitely many) Euler angles that are at least perpendicular (i.e. the rotation R is 90º different) and at worst oriented in opposite directions (i.e. F and -F have the same screen orientation).
Use a well-reviewed asset store package instead, or study an open source solution like Parallax instead.
There is no correct answer for how to interpret a 3-dimensional gyroscope measurement as a 4-dimensional orientation, it is a qualitative decision based on your needs. However most games assume that players will be holding their device landscape with the forward direction parallel to the Earth, rotating about that forward (i.e. like driving a car), so that's what most documentation / tutorials online will describe and what you will find is most robust.

Wrong axis values of Gamepad in Matlab's Psychtoolbox

I'm using the Gamepad interface in Psychtoolbox together with a Logitech Attack3 joystick and the following code:
while ~Gamepad('GetButton', 4, 1)
force = Gamepad('GetAxis', 4, 2);
force = force / 32768;
zoomFactor = 0.1 * force;
zoom(1 + zoomFactor);
end
It's supposed to get the vertical axis value from the joystick and use that to calculate the zoom factor (toy problem: zoom in and out of picture).
When querying the axis value, I get strange results. If I move the joystick, the axis value changes as expected. However, when I release the joystick back into resting, the axis value should return 0, but it just stays at the last displayed value. Basically, the joystick only registers movement away from the center but not the returning motion back to the resting position.

Unity - get position of UI Slider Handle

I am working on Unity 4.7 project and need to create shooting on the target. I simulated gunpoint using horizontal and vertical slider moving on the time. When I click the button I need to memorize x and y coordinates of handles and instantiate bullet hole at this point but don't know how to get cords of sliders handle. It is possible to get values but it seems that it doesn't correspond to coordinates. If horizontal slider changes its value for 1, would its handle change x position for 1?
Use this then:
public static Vector3 GetScreenPositionFromWorldPosition(Vector3 targetPosition)
{
Vector3 screenPos = Camera.main.WorldToScreenPoint(targetPosition);
return screenPos;
}
Have the reference to Handles of the horizontal and vertical sliders, and use them like:
Vector3 pos = GetScreenPositionFromWorldPosition(horizontalHandle.transform.position);

Orientations Offset

I am using the MVN-Unity plug-in to access Xsens motion capture data and animate a Unity character in real-time. The character in white and green and yellow is a Unity skeleton that is animated based on Xsens motion data.
I am now trying to animate a different (non-Unity) character using Xsens (the other character that looks like a human) so similar to what the plug-in does, the motion data (positions & orientations) are being mapped to his joints/bones.
But as you can see below, something is wrong with orientations...
I think the reason might be that the rotations from MVN are not properly offset. As you can see in the next two pictures, the MVN hips have the x-axis (red) pointing to the puppet's backside, whereas for the guy's hips, the x-axis points to the right of him.
It might also be that the plug-in is using global rotations somewhere where it should use local rotations. That this must be the case can be demonstrated when I rotate the guy around before I start the Unity app; i.e. select the guy's root game object and try setting the y-rotation to 0/90/180/270 before pressing play, and compare the results: every time the distortions are different.
I don't know how to properly fix this. The code snippet that updates the Unity model (mapped to the MVN puppet or the guy) is as follows. I took this from the plug-in scripts:
private void updateModel(Transform[] pose, Transform[] model)
{
// Re-set the target, then set it up based on the segments.
Vector3 pelvisPos = new Vector3();
Vector3 lastPos = target.position;
target.position = Vector3.zero;
// Map only 23 joints.
for (int i = 0; i < 23; i++)
{
switch (i)
{
// Position only on y axis, and leave x and z to the body. Apply the 'global' position & orientation to the pelvis.
case (int)XsAnimationSegment.Pelvis:
pelvisPos = pose[i].position * scale;
model[i].position = new Vector3( model[i].position.x, pelvisPos.y, model[i].position.z );
model[i].rotation = pose[i].rotation * modelRotTP[i];
break;
// Update only the 'orientation' for the rest of the segments.
default:
if ( model[i] != null )
{
model[i].rotation = pose[i].rotation * modelRotTP[i];
}
break;
}
}
// Apply root motion if the flag is enabled; i.e. true.
if (applyRootMotion)
{
// Only update x and z, since pelvis is already modified by y previously.
target.position = new Vector3(pelvisPos.x + pelvisPosTP.x, lastPos.y, pelvisPos.z + pelvisPosTP.z);
}
// Set the final rotation of the full body, but only position it to face similar as the pelvis.
Quaternion q = Quaternion.Inverse(modelRotTP[(int)XsAnimationSegment.Pelvis]) * model[(int)XsAnimationSegment.Pelvis].rotation;
target.rotation = new Quaternion(target.rotation.x, q.y, target.rotation.z, target.rotation.w);
}
I sort of understand what the code does, but I don't know how to fix this problem. Most probably to do with the axes being different? I would appreciate any help...
You can modify XsLiveAnimator.cs script in the line: 496
with that
model[segmentOrder[i]].transform.rotation = orientations[segmentOrder[i]];
model[segmentOrder[i]].transform.Rotate(rotOffset, Space.World);
rotOffset is a Vector3 of your rotation