Search code examples
iosswiftsprite-kitaccelerometercore-motion

Decreasing accelerometer's sensitivity


I am making a game which have a square that moves from left to right, and at first I was using applyForce to do so, but I didn't like the results because it was not sensitive enough. Then I changed to velocity:

func motionSensorUpdate(currentTime: CFTimeInterval){

    let square = childNodeWithName(ksquarename) as! SKSpriteNode

    updateSquare(currentTime, Square: square)

    if let data = motionManager.accelerometerData {

        if CGFloat(data.acceleration.x) > 0.2 {
            square.physicsBody!.velocity = CGVectorMake(200, -200)
        } else if CGFloat(data.acceleration.x) < 0.2 {
            square.physicsBody!.velocity = CGVectorMake(-200, -200)
        }
    }
}

The problem now is that it's too sensitive. So, if I'm not moving the cellphone it would still move the object. How do I fix it without moving back to applyforce?


Solution

  • Try:

    let multiplier = 200.0
    let x = data.acceleration.x
    
    if x > 0.2 {
        square.physicsBody!.velocity = CGVector(dx: x * multiplier, dy: -200)
    } else if x < 0.2 {
        square.physicsBody!.velocity = CGVector(dx: x * multiplier, dy: -200)
    }
    

    The above will vary the speed of the node depending on how much the device is tiled; just change multiplier to change the sensitivity.