- Home /
Question by
Klaudia · Jul 31, 2012 at 05:56 PM ·
androidcaraccelerometerracing game
Acelerometer Controlled Racing Game For Android
Hello,
I'm working on racing game for android. How can I make my car accelerometer controlled? I'm more graphic designer than programer so I will be very happy if someone helps :)
Klaudia
// These variables allow the script to power the wheels of the car.
var FrontLeftWheel : WheelCollider;
var FrontRightWheel : WheelCollider;
// These variables are for the gears, the array is the list of ratios. The script
// uses the defined gear ratios to determine how much torque to apply to the wheels.
var GearRatio : float[];
var CurrentGear : int = 0;
// These variables are just for applying torque to the wheels and shifting gears.
// using the defined Max and Min Engine RPM, the script can determine what gear the
// car needs to be in.
var EngineTorque : float = 230.0;
var MaxEngineRPM : float = 3000.0;
var MinEngineRPM : float = 1000.0;
private var EngineRPM : float = 0.0;
function Start ()
{
rigidbody.centerOfMass += Vector3(0, -.75, .25);
}
function Update ()
{
// finally, apply the values to the wheels. The torque applied is divided by the current gear, and
// multiplied by the user input variable.
FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
// the steer angle is an arbitrary value multiplied by the user input.
FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
}
Comment
Answer by HunterKrech · Aug 22, 2012 at 01:02 AM
Hey here is what i used for my 2d ball game using the accelerometer. BTW throw this under function update.
var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y* 1; //the integer is speed
transform.position.x += dir.x;
This is only on the x axis though. If you want it to be on the z as well, considering its a car game you'd want to add this underneath it.
var dir : Vector3 = Vector3.zero;
dir.x = -Input.acceleration.y* 1;
transform.position.z += dir.z;