[resolved] accelerometer to affect gravity or world rotation

Hello everybody!
Fede here, from italy: new to the forum and to unity as well :slight_smile:

So…i’m currently working at a studio, where i have to experiment with Unity and his potential applications on the iPhone. I have only some actionscript background, but so far the transition to 3d went really smooth.

Yeah, so far.

The task i am trying to accomplish right now is to tilt an object (let’s say it’s an arrow), so that it always points upwards in the real world, of course using the accelerometer.
Seems like an easy task, right???

first try was:

function Update () {
	var roty=iPhoneInput.acceleration.y;
        transform.rotation.z=roty
}
//the real code is longer and has accelerometer input smoothed, but for simplicity's sake i cut it out ^_^

it works. Howewer, the accelerometer readings from y range from -1 to 1, where -1 is “home button down” and 1 is “home button up”. The problem is that “home button right” and “home button left” share the same reading (0), and i cannot tell wether the user is holding the iphone with the home button left or right. Therefore, the arrow will point upwards only for 180 degrees-for the next 180 it will point to the ground.

So, i understood i have to incorporate readings from PhoneInput.acceleration.x

second try is:

var up = Mathf.Atan2(iPhoneInput.acceleration.y, iPhoneInput.acceleration.x);
transform.LookAt (up);
//once again, bare bone code ^_^

Guess what? Unity error: No appropriate version of ‘UnityEngine.Transform.LookAt’ for the argument list ‘(float)’ was found.
of course, it’s because there is no transform component to look at; but i have no idea of how i could transmit the vector resulting from Mathf.Atan to a transform component. :frowning:

So, i have finished my options. Anyone could please put me on the right trail?
Thanks in advance :slight_smile:
Fede

Changed the title to help google searchers in the same situation as me (or should i say: my past me ^_^)

My solution:

Here is the uncommented code, if you need to copy/paste:

private var up=Mathf.Atan2(iPhoneInput.acceleration.y,iPhoneInput.acceleration.x)*Mathf.Rad2Deg; 
private var rotay = up; 
var smoothTime = 0.3;
var smoothVelocity = 0.3; 

function Update () { 
up=Mathf.Atan2(iPhoneInput.acceleration.y,iPhoneInput.acceleration.x)*Mathf.Rad2Deg; 
rotay = Mathf.SmoothDampAngle(rotay, up, smoothVelocity, smoothTime); 
transform.eulerAngles.z = rotay;
}

very simple, but (at least for me), hard to come up with :slight_smile:
hope it helps,
fede

1 Like

I’m not sure I understand how this works, why you need two acceleration axes to get one rotation axis.

Your code works great for rotating around one axis, but I want to rotate an object so that it always points down, relative to the ground, as if it’s hanging from the center of the screen. So I need to rotate in two axes, but I’m not sure how to adapt this code. I can get it to rotate in either the X or Z axis, but not in both.

I just used the Controller from Unity’s Roll-a-ball iPhone example: http://unity3d.com/support/resources/example-projects/iphone-examples.html

I find it much simpler and I even simplified it for me:

	public float force = 9.8f;

	void FixedUpdate ()
	{
		dir.x = Input.acceleration.x;
		dir.y = Input.acceleration.y;
		Physics.gravity = dir * force;
	}