- Home /
Accelerometer question
I am developing an game similar to Doodle Jump, where the character continually jumps upwards from platform to platform. The user controls the character by tilting his device left and right. There is a very specific effect that I am trying to achieve, and I'm going to have to ask for help to pull it off.
The character's position on the screen should be determined by how the player is tilting the device. When the device is held upright, the character should be at the center of the screen. When the device is tilted left or right, the character should smoothly move left or right, but he should always return to the center of the screen when the device is held upright again.
I've tried to achieve this effect using rigidbody.AddForce and rigidbody.velocity, but neither of those two methods produces the desired effect.
How would I make an object move left and right when tilting a device, and return to the center of the screen when the device is not being tilted? My attempts to pull this off have been catastrophically bad, so my approach is probably wrong. Before I waste any more time barking up the wrong tree, I've come here to ask: How would I accomplish the kind of behavior that I'm describing?
using Input.acceleration.y you will get angle (+/-) of tilt and get the multipliers for smoothing the character movement.
Answer by aldonaletto · Jun 01, 2012 at 09:23 AM
1- Using Input.acceleration: The accelerometer reports the current acceleration in G's as a Vector3 in its own coordinate system (the Z axis is reversed relative to Unity's):
If you want to run your game in landascape orientation (home button at the right hand), the docs' Input.acceleration example is a good starting point. This example assumes that the screen shows the XZ plane (a typical top-down configuration). If in your game the screen shows the XY plane instead (a typical side-scroller), the code should be modified a little, like below - it's also a good idea to filter the acceleration data (it's too jerky without filtering):
// Move object in XY using accelerometer (home button at right hand) var speed: float = 6.0; var filter: float = 5.0;
private var accel: Vector3;
function Start(){
accel = Input.acceleration;
}
function Update(){
// filter the jerky acceleration in the variable accel:
accel = Vector3.Lerp(accel, Input.acceleration, filter * Time.deltaTime);
// map accel -Y and X to game X and Y directions:
var dir = Vector3(-accel.y, accel.x, 0);
// limit dir vector to magnitude 1:
if (dir.sqrMagnitude > 1) dir.Normalize();
// move the object at the velocity defined in speed:
transform.Translate(dir * speed * Time.deltaTime);
}
2- Character position in the screen: The behaviour you describe looks more like a camera follow script (like SmoothFollow), which will follow the player when it's trying to go out of screen.
That was an extremely in-depth answer, Aldo! I can see why you have almost 23,000 karma.
This script isn't exactly what I was looking for, because it doesn't return the gameObject to the center of the screen when the device is held without any tilt. However, it definitely helps me learn how to fix the jerky acceleration. I'll try my hand at modifying this script to see if it'll suit my needs. If I succeed, I'll come back and mark this question as answered!
This was exactly what I was looking for, big thanks for the code snippet, could you please help me in limiting the movement within a range? Say only -5 to + 5 not beyond it.
Limit the object position? If so, you can clamp the object position after translating it: define the max and $$anonymous$$ X and Y coordinates and use $$anonymous$$athf.Clamp - like this:
var maxX = 5;
var $$anonymous$$X = -5;
var maxY = 5;
var $$anonymous$$Y = -5;
...
// move the object at the velocity defined in speed:
transform.Translate(dir * speed * Time.deltaTime);
var pos = transform.position;
// clamp new position:
pos.x = $$anonymous$$athf.Clamp(pos.x, $$anonymous$$X, maxX);
pos.y = $$anonymous$$athf.Clamp(pos.y, $$anonymous$$Y, maxY);
// and assign it to the object:
transform.position = pos;
}
This code limits the object movement to an axis aligned rectangle defined by $$anonymous$$X, maxX, $$anonymous$$Y and maxY.
Thanks a lot again for the direction. It practically worked really smooth, I have never used clamp before and it gave me a smooth limiting feel.
I know I m too late :"(
Bt still can I get my confusion cleared. . I am using this script to move my player on X axis and it's working properly . I also want to rotate my object at certain angle same time while it's moving . Like if it is moving from right to left, rotate left And if it move left to right ,rotate right Can u please help me how to do this and what function I should use to do this with above script :")
Your answer
Follow this Question
Related Questions
how to convert Input.mousePosition into iphone/android accelerometer? 1 Answer
tilt control for jump 0 Answers
Add tilt function to rotate a ball android device 0 Answers
Making player object jump other than tapping the screen in android phones 1 Answer
The build version of my android game doesn't work (in unity remote it works perfectly) 0 Answers