x


Using the accelerometer to move left and right on one axis

Hi there, I'm extremely new to UnityIphone and I'm trying to do something that I would consider very simple.

I'm trying to make a object 'strafe' left and right depending on the tilt of the accelerometer, I don't even want velocity to come into play, simply move left at a constant speed in tilted left, and visa-versa.

Thanks - Caius

more ▼

asked Aug 09 '10 at 03:05 PM

Caiuse gravatar image

Caiuse
787 83 103 121

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
var speed : float = 10;
var moveThreshold : float = .2;

private var movex : float; 
private var iPx : float; 

function Update()
{
    movex = 0;
    iPx = iPhoneInput.acceleration.x;

    if (Mathf.Abs(iPx) > moveThreshold)
    {
        movex = Mathf.Sign(iPx) * speed;
        transform.Translate(movex,0,0);
    }
}

The variable moveThreshold is used so that your object will remain still when the device is upright and will only move when you lean the device enough.

Mathf.Sign() outputs a +1, 0 or -1, whereas Mathf.Abs() returns a positive number only.

http://unity3d.com/support/documentation/ScriptReference/Mathf.Sign.html http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

more ▼

answered Aug 09 '10 at 11:04 PM

Billamu gravatar image

Billamu
277 4 6 16

Thank you so much

Aug 10 '10 at 10:44 AM Caiuse
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x520
x224
x139

asked: Aug 09 '10 at 03:05 PM

Seen: 6938 times

Last Updated: Aug 09 '10 at 03:05 PM