Create a gist now

Instantly share code, notes, and snippets.

@kihira /PlayerPlane.cs Secret
Created Oct 26, 2015

What would you like to do?
// Accelerometer Settings
public float minXTiltAngle = 5f;
public float minYTiltAngle = 5f;
public Vector3 acceleration = Vector3.zero;
private Vector3 accelOffset = Vector3.zero;
void Start()
{
// Sets the offset based upon on how the game starts. This allows the player to hold the device at an angle comfortable for them whilst still being able to have full movement controls
// TODO some way for the player to reset this or find average over a certain time and use that?
accelOffset = Input.acceleration;
accelOffset.x = 0;
}
void Update()
{
Movement();
}
/// <summary>
/// This method handles all movement for this plane based upon keyboard or accelerometer input
/// Should be called from the Update method
/// </summary>
private void Movement()
{
// Get input from acceleration on device
// Values of Input.acceleration are at -1 when device is tilted 90 one way and +1 when tilted 90 the other way
// The accelOffset is the position that the player held the device when starting the game. As such, we remove it from the current acceleration to try to get only get the movement change
Vector3 translation = Input.acceleration - accelOffset;
// This offsets the input from the device by the minimum required angle so when the required angle is hit, it is not a sudden acceleration but more of a smooth one as if increasing from 0
if (translation.x > 0)
{
translation.x = Mathf.Clamp(translation.x - (minXTiltAngle / 90), 0, 1);
}
else if (translation.x < 0)
{
translation.x = Mathf.Clamp(translation.x + (minXTiltAngle / 90), -1, 0);
}
if (translation.y > 0)
{
translation.y = Mathf.Clamp(translation.y - (minYTiltAngle / 90), 0, 1);
}
else if (translation.y < 0)
{
translation.y = Mathf.Clamp(translation.y + (minYTiltAngle / 90), -1, 0);
}
// Effectively multiples the two vectors together
translation.Scale(acceleration);
//translation.Set(Mathf.Clamp(translation.x, -1, 1), Mathf.Clamp(translation.y, -1, 1), 0);
// Override acceleration with input from arrow keys if needed
if (Input.GetKey(KeyCode.RightArrow))
{
translation = new Vector3(1, 0, 0);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
translation = new Vector3(-1, 0, 0);
}
else if (Input.GetKey(KeyCode.UpArrow))
{
translation = new Vector3(0, 1, 0);
}
else if (Input.GetKey(KeyCode.DownArrow))
{
translation = new Vector3(0, -1, 0);
}
// Times the translation by the movement speed and account for delta time
// TODO: Cap the speed of the plane
translation *= moveSpeed * Time.deltaTime;
// Check if the plane is within bounds and that the movement is trying to make them go away from the bounds, into the main game area
if ((transform.position.x > 5 && translation.x > 0) || (transform.position.x < -5 && translation.x < 0))
{
translation.x = 0;
}
if ((transform.position.y < -5 && translation.y < 0) || (transform.position.y > -2 && translation.y > 0))
{
translation.y = 0;
}
transform.Translate(translation);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment