- Home /
Question by
JBak96 · Feb 19, 2013 at 12:40 PM ·
movementvector3accelerometerlimit
Limit object position which is moved by accelerometer
Hello Everybody!
For my android game I control an object with the accelerometer along the x-axis. It works all fine. My problem is to limit the movement at the edges e.g that the object can only move between -10 and 10.
Here's my script:
// Move object using accelerometer
var speed = 10.0;
function Update () {
var dir : Vector3 = Vector3.zero;
// we assume that device is held parallel to the ground
// and Home button is in the right hand
// remap device acceleration axis to game coordinates:
// 1) XY plane of the device is mapped onto XZ plane
// 2) rotated 90 degrees around Y axis
dir.x = Input.acceleration.y;
// clamp acceleration vector to unit sphere
if (dir.sqrMagnitude > 1)
dir.Normalize();
// Make it move 10 meters per second instead of 10 meters per frame...
dir *= Time.deltaTime;
// Move object
transform.Translate (dir * speed);
}
I hope that anyone can help.
Greetings from Germany! Josef
Comment
Try either of these untested solutions:
transform.position.x=$$anonymous$$athf.Clamp(-10,10);
or
if((transform.position.x<10&&Input.acceleration.y>0)||(transform.position.x>-10&&Input.acceleration.y<0))
transform.Translate (dir * speed);
(Or both)
Your answer
Follow this Question
Related Questions
How to set maximum distance for a player going left and right? 1 Answer
Increasing the speed of an object when the scale is decreased and vice versa 0 Answers
How to smooth my camera (Vector3.Lerp) 0 Answers
2D Vector Movement,Vectorel Movement with Buttons 2 Answers
How can I calibrate phone accelerometer? 0 Answers