- Home /
Making player object jump other than tapping the screen in android phones
Hello guys!
I was just wondering if there was a way to make a player object jump by using an accelerometer. This question just popped in my head a few days ago and it feels pretty rad to make the player jerk their phone up just to make their player object jump. So far most examples I've seen uses a touch/tap event to make the jump.
My player controller goes a little like this:
void Start(){
rb = GetComponent<Rigidbody> ();
}
void Update(){
Vector3 tilt = new Vector3(Input.acceleration.x, Input.acceleration.y, -Input.acceleration.z);
tilt = Quaternion.Euler (90,0,0) * tilt;
rb.AddForce (tilt * 4000);
}
The game I'm making is like a roll-a-ball whose orientation is in landscape, back of the phone parallel to the floor and home button on the right.
So is there any possible way to make that happen?
Any comment, reply is appreciated!
Thanks in advance!
Answer by jchester07 · Oct 24, 2017 at 05:52 AM
You can do this
public float jumpMultiplier;
public float jumpTreshold;
void Update(){
if(Input.accelerometer.normalized.y * jumpMultiplier >= jumpTreshold) {
// Jump!!
}
}
Sorry for the late reply! Would something like this work?
public float jump$$anonymous$$ultiplier = 10;
public float jumpTreshold;
void Update(){
if(Input.accelerometer.normalized.y * jump$$anonymous$$ultiplier >= jumpTreshold) {
rigidbody.AddForce(Vector3.up * jump$$anonymous$$ultiplier, Force$$anonymous$$ode.Impulse);
}
}
Oh and how can I get the jumpTreshold?
yes you can do it that way. but I suggest to create a different variable for for the jump ins$$anonymous$$d of using the jump$$anonymous$$ultiplier. The jump$$anonymous$$ultiplier is for the accelerometer and not necessarily the force you want to apply. the jumpTreshold should be equal or lower than the jump$$anonymous$$ultiplier. Basically, jumpTreshold would function as a sensitivity for the accelerometer.