- Home /
why cant I get android accelerometer working in two planes?
Ive searched high and low to no avail! Im New to unity and as such am experimenting using android accelerometer to roll a sphere around my phone display. I found some code that seems to work for others, but for me I only get tilt in z axis (wrt display, x axis wrt main camera) but I require tilt motion control for left, right, back and forth. Probably something really simple i've overlooked, so feeling foolish in anticipation...
Any help will be greatly appreciated!
Here is essentially the code im using;
Vector3 dir = Vector3.zero ;
dir.x = - Input.acceleration.y ;
dir.z = Input.acceleration.x ;
if ( dir.sqrMagnitude > 1 )
dir.Normalize();
dir *= Time.deltaTime;
transform.Translate ( dir * speed );
Vector3 dir = Vector3 .zero ;
dir. x = - Input. acceleration . y ;
dir. z = Input .acceleration . x ;
if ( dir. sqr$$anonymous$$agnitude > 1 )
dir . Normalize();
dir *= Time . deltaTime;
transform. Translate ( dir * speed );
Using a HTC? You accelerometer may be broken, or needs calibrating.
That's 63 people who have potentially viewed your question looking to answer it :)
can anyone see where im going wrong? dont get me wrong.. im not expecting to have my hand held atm but I am green lol
Answer by Graham-Dunnett · Jul 04, 2014 at 03:10 PM
meat5000... im on a Sony c1505. I have considered calibrating the accelerometer since I get some weird things happening if, say, I hold the phone upright when loading the app.. methinks I need to zero it first, so thanks and ill keep it in $$anonymous$$d :-)
lets see if I can post today.. big thanks to boredmormon and graham for ur help. ill see if that code solves my issue
Try
Debug.Log(Input.acceleration);
This should show you if you have a hardware issue.
Also, consider this
http://docs.unity3d.com/ScriptReference/Gyroscope.html
Accelerometer is supposed to measure changes in position, literally the acceleration of the device. Gyroscope, in fact, is what shows you the orientation.
using UnityEngine;
using System.Collections;
public class tilty : $$anonymous$$onoBehaviour {
//Use this for initialization
void Start ()
{
var tilt : Vector3 = Vector3.zero;
var speed : float;}
private var circ : float;
private var previousPosition : Vector3;
@script RequireComponent(Rigidbody);
function Start()
{
//Find the circumference of the circle so that the circle can be rotated the appropriate amount when rollin
circ = 2 * $$anonymous$$athf.PI * collider.bounds.extents.x;
previousPosition = transform.position;
}
function Update (){
tilt.x = Input.acceleration.y;
tilt.z = -Input.acceleration.x;
rigidbody.AddForce(tilt * speed * Time.deltaTime);
}
function LateUpdate(){
var movement : Vector3 = transform.position - previousPosition;
c:\Users\DAVE\Documents\New Unity Project 3\Assets
\tilty.cs: Error CS1519: Invalid token ';' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp)
movement = Vector3(movement.z,0, -movement.x);
transform.Rotate(movement / circ * 360, Space.World);
previousPosition = transform.position;
}}
// Update is called once per frame
//void Update () {
//}
//}
Added this code... got 5 error messages:
c:\Users\DAVE\Documents\New Unity Project 3\Assets\tilty.cs: Error CS1001: Identifier expected (CS1001) (Assembly-CSharp)
c:\Users\DAVE\Documents\New Unity Project 3\Assets\tilty.cs: Error CS1519: Invalid token ';' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp)
c:\Users\DAVE\Documents\New Unity Project 3\Assets\tilty.cs: Error CS1002: ; expected (CS1002) (Assembly-CSharp)
c:\Users\DAVE\Documents\New Unity Project 3\Assets\tilty.cs: Error CS1519: Invalid token ';' in class, struct, or interface member declaration (CS1519) (Assembly-CSharp)
c:\Users\DAVE\Documents\New Unity Project 3\Assets\tilty.cs: Error CS1002: ; expected (CS1002) (Assembly-CSharp)
Have tried tweaking.. no joy. i dont expect anyone to hold my hand thru this but i really am scuppered atm!!
Answer by dare00 · Jul 12, 2014 at 03:06 PM
Ok I found this: http://u3d.as/content/unity-technologies/star-trooper/5fK and with some tweaking it does what I need! :-)