- Home /
Question by
looMeenin · Jul 31, 2014 at 05:53 PM ·
c#optimizationaccelerometer
How to smooth my accelerometer-based character movement?
I am building an infinite vertical platformer for mobile platforms and I am using the accelerometer to move the player left and right. The further the device is tilted the faster the player moves across the screen. Currently my player is a little too shakey and I would like to create a more fluid movement across the screen. Here is my code to move the character:
/********************************* Variables **************************************/
// Variables
float jumpForce = 700f;
float maxSpeed;
float acceleration;
/********************************* Start Method ************************************/
void Start ()
{
acceleration = Mathf.Abs (Input.acceleration.y);
}
/********************************* Fixed Update **************************************/
void FixedUpdate () {
if (acceleration < 0.2f) {
maxSpeed = 17;
} else if (acceleration < 0.9f) {
maxSpeed = 25;
} else {
maxSpeed = 40;
}
float move = Input.acceleration.x;
rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
/******************************* Collision Function ******************************/
void OnCollisionEnter2D(Collision2D coll)
{
foreach(ContactPoint2D contact in coll.contacts)
{
rigidbody2D.AddForce (new Vector2(0, jumpForce));
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is my camera motion so jittery? 3 Answers
How to optimize the build game for unity webplayer? 1 Answer