Created
July 6, 2017 19:13
-
-
Save anonymous/c8b06e196d7f5faf89232bc98bc2bd0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/** | |
* This is a class inside of another class. Made (and System.Serializable) are there so the | |
* boundaries numbers can be inputted publically in unity but the option is collapsable to | |
* keep clutter down. | |
* | |
* It does not have " : MonoBehavior" after the class name so it doesn't inheret anything. | |
* | |
* Creates the variables fore the boundaries of the game screen. | |
*/ | |
[System.Serializable] | |
public class Boundary{ | |
public float xMin, xMax, zMin, zMax; | |
} | |
public class PlayerController : MonoBehaviour { | |
private Rigidbody rb; | |
/* | |
* Controls how fast the ship goes. | |
* | |
* Is public, so speed is set in unity itself. | |
*/ | |
public float speed; | |
//Creates a tilt in the ship's sprite when hitting the boarder. | |
public float tilt; | |
//Creates an instance of the Boundary class | |
public Boundary boundary; | |
//Instance of a bolt | |
public GameObject shot; | |
public Transform shotSpawn; | |
public SimpleTouchPad touchPad; | |
public float fireRate; | |
private float nextFire; | |
private GameObject shotFired; | |
private Quaternion calibrationQuaternion; | |
private GameObject gameObj; | |
//Data created when the game loads. | |
void Start(){ | |
rb = GetComponent<Rigidbody>(); | |
CalibrateAccellerometer(); | |
gameObj = GameObject.FindWithTag("SimpleTouchPad"); | |
if (gameObj != null) | |
{ | |
touchPad = gameObj.GetComponent<SimpleTouchPad>(); | |
} | |
} | |
void Update(){ | |
if (Input.GetButton("Fire1") && Time.time > nextFire) | |
{ | |
nextFire = Time.time + fireRate; | |
GetComponent<AudioSource>().Play(); | |
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);// as GameObject; | |
} | |
} | |
//Moves things using physics | |
void FixedUpdate(){ | |
// float moveH = Input.GetAxis("Horizontal"); | |
// float moveV = Input.GetAxis("Vertical"); | |
// Vector3 movement = new Vector3(moveH, 0.0f, moveV); | |
// Vector3 accelerationRaw = Input.acceleration; | |
// Vector3 acceleration = FixAccelleration(accelerationRaw); | |
// Vector3 movement = new Vector3(acceleration.x, 0.0f, acceleration.y); | |
Vector2 direction = touchPad.GetDirection(); | |
Vector3 movement = new Vector3(direction.x, 0.0f, direction.y); | |
rb.velocity = movement * speed; | |
rb.position = new Vector3( | |
Mathf.Clamp(rb.position.x, boundary.xMin, boundary.xMax), | |
0.0f, | |
Mathf.Clamp(rb.position.z, boundary.zMin, boundary.zMax) | |
); | |
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt); | |
} | |
void CalibrateAccellerometer (){ | |
Vector3 accelerationSnapshot = Input.acceleration; | |
Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0.0f, 0.0f, -1.0f), accelerationSnapshot); | |
calibrationQuaternion = Quaternion.Inverse(rotateQuaternion); | |
} | |
Vector3 FixAccelleration(Vector3 acceleration){ | |
Vector3 fixedAcceleration = calibrationQuaternion * acceleration; | |
return fixedAcceleration; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment