- Home /
Using Mathf.Clamp with Accelerometer to control movement speed has slow response time, how can I smooth it out?
Hi guys, first question on UA. Im a first year game design student with a moderate amount of coding experience, who is now undertaking a project for an internship, and my expertise is lacking ^^.
The situation: a 2D side scrolling game where the player's speed will be boosted if the mobile device is tilted in a particular direction.
I have never made a mobile game so I have been using a lot of example code and reverse engineering it to understand how to control the functionality.
ie. this is not my code, it was taken from an example and slightly modified to work in my circumstance.
Code is as follows :
var zeroAc : Vector3;
var curAc : Vector3;
var sensH = 15.0;
var sensV = 15.0;
var smooth = 0.0;
var getAxisH = 0.0;
var getAxisV = 0.0;
var isRight=false;
var isLeft=false;
var isUp=false;
var isDown = false;
//*******************This is the part of the code which i need help with***************
function Update () {
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
getAxisV = Mathf.Clamp(-curAc.x * sensV, -1, 1);
getAxisH = Mathf.Clamp(-curAc.y * sensH, -1, 1);
if(getAxisH==-1)
{ isLeft = true; }
else if(getAxisH==1)
{ isRight = true; }
else {isLeft = false;
isRight = false;}
if(getAxisV==1)
{ isUp = true; }
else if(getAxisV==-1)
{ isDown = true; }
else {isUp=false;
isDown=false;}
}
///*********The following is pseudo-code for the effect of tilting the device***********
if (isRight){
Player Speed = doubled;
}
else {
Player Speed = normal;
}
Now the effect of doubling the speed works just fine (unless I copied the code into this question wrong), however the issue is with the clamping.
When the device is tilted sufficiently to the right the value getAxisH hits 1 and is correctly calmped at that value.
Now 2 issues arise. First of all, the range at which var isRight is no longer true is very small and quite hard to find without over shooting into isLeft, which is undesirable in this case. Obviously i could extend the range but then we would have to tilt further to go into isRight, also undesirable.
The second issue is that the response time between the device tilting and the game responding is quite big, thus the effect is delayed substantially, which all but defeats its purpose in this game.
So in conclusion my questions are:
1. Is there a convenient way of reacting instantly to direction of tilt, and not magnitude in one direction or another?
2. Is the lag inherent to accelerometers in mobile devices, or is there something up with my nexus 7/laptop that the response time is something like half a second (massive for a reaction based scrolling game)?
As I say, I am quite inexperienced with game programming at this point, therefore I humbly welcome all criticisms and pointers, related to my questions or otherwise, even if it is just to say that the question is stupid ^^ I also apologise if the answer is self-evident and yet eludes me. I have terrible sub-nasal vision.
Thank you all for reading and I hope someone can help.
Cheers