76 lines
2.3 KiB
C#
76 lines
2.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[System.Serializable]
|
|
public class Boundary
|
|
{
|
|
public float xMin, xMax, zMin, zMax;
|
|
}
|
|
|
|
public class PlayerController : MonoBehaviour
|
|
{
|
|
public float speed, tilt, fireRate;
|
|
private float nextFire;
|
|
public Boundary boundary;
|
|
public GameObject shot;
|
|
public Transform shotSpawn;
|
|
public SimpleTouchPad touchPad;
|
|
public SimpleTouchAreaButton areaButton;
|
|
private Rigidbody rb;
|
|
private AudioSource fireSound;
|
|
private Quaternion calibrateQuaternion;
|
|
|
|
void Start()
|
|
{
|
|
CalibrateAccellerometer();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (areaButton.CanFire() && Time.time > nextFire)
|
|
{
|
|
nextFire = Time.time + fireRate;
|
|
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
|
|
fireSound = GetComponent<AudioSource>();
|
|
fireSound.Play();
|
|
}
|
|
}
|
|
void FixedUpdate ()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
|
|
// float moveHorizonal = Input.GetAxis("Horizontal");
|
|
// float moveVertical = Input.GetAxis("Vertical");
|
|
// Vector3 movement = new Vector3(moveHorizonal, 0.0f, moveVertical);
|
|
|
|
// Vector3 accelerationRaw = Input.acceleration;
|
|
// Vector3 acceleration = FixAcceleration(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);
|
|
calibrateQuaternion = Quaternion.Inverse(rotateQuaternion);
|
|
}
|
|
|
|
Vector3 FixAcceleration (Vector3 acceleration)
|
|
{
|
|
Vector3 fixedAcceleration = calibrateQuaternion * acceleration;
|
|
return fixedAcceleration;
|
|
}
|
|
} |