![alt text][1]
Hello. I am working on a motocross game but I have been stuck for a really long time on rotating objects with the accelerometer. I have been putting it off and working on other things but it is now a vital part of me completing my game. In real life riding the bike acts like a gyro. When you give it gas it rotates backwards and opposite when you put on the brakes. I am wanting to do the same with the accelerometer but tilting the phone forward to lean forward and back to lean backward. I converted the steering to use the accelerometer but I have no clue how I did it because it has been so long ago.
I know that if(!grounded) I want to enable the tilt in either direction, forward or back. However I do now know whether to use transform.rotate() or rigidbody.moveRotation() and how I would implement that with the accelerometer.
public Vector3 zeroAc;
public Vector3 curAc;
public float yTilt;
public float sensH = 2f;
public float sensV = 2f;
//public float sensD = 2f;
public float smooth = 0.4f;
public float GetAxisH = 0.000f;
public float GetAxisV = 0.000f;
//public float GetAxisD = 0.000f;
public bool hasInAirControl = false;
Here are my accelerometer variables. I am using sensH and GetAxisH for my steering(horizontal).
if (!crash) {
steer = Mathf.MoveTowards (steer, GetAxisH/*Input.GetAxis("Horizontal")*/, 0.1f);
accel = GetAxisV;//Input.GetAxis("Vertical");
brake = Input.GetButton ("Jump");
shift = Input.GetKey (KeyCode.LeftShift) | Input.GetKey (KeyCode.RightShift);
} else {
steer = 0;
}
} else if (controlMode == ControlMode.touch) {
if (accelFwd != 0) {
accel = accelFwd;
} else {
accel = accelBack;
}
steer = Mathf.MoveTowards (steer, steerAmount, 0.07f);
}
and my steering setup...
public void BikeSteer (/*float amount*/)
{
//accelerometer
curAc = Vector3.Lerp (curAc, Input.acceleration - zeroAc, Time.deltaTime / smooth);
GetAxisV = Mathf.Clamp (curAc.y * sensV, -1, 1);
//Debug.Log("v" + GetAxisV);
GetAxisH = Mathf.Clamp (curAc.x * sensH, -1, 1);
//Debug.Log("h" + GetAxisH);
// now use GetAxisV and GetAxisH instead of Input.GetAxis vertical and horizontal
// If the horizontal and vertical directions are swapped, swap curAc.y and curAc.x
// in the above equations. If some axis is going in the wrong direction, invert the
// signal (use -curAc.x or -curAc.y)
//////
steerAmount = GetAxisH;
}
and here is my steering function.
Like I said I dont really know how I converted button steering to accelerometer because I havent touched my code in a few months. Any help would be greatly appreciated!
[1]: /storage/temp/56492-screen-shot-2015-10-19-at-44711-pm.png
↧