88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
public class BallController : MonoBehaviour {
|
||
|
|
public Joystick joy;
|
||
|
|
public float acceleration = 1.0f;
|
||
|
|
public Camera cam;
|
||
|
|
public float maxAngularVelocity = 7;
|
||
|
|
public CheckpointManager manager;
|
||
|
|
public AudioClip floorContactSound;
|
||
|
|
public AudioClip rollingSound;
|
||
|
|
private AudioSource contactSource;
|
||
|
|
private AudioSource rollingSource;
|
||
|
|
// Use this for initialization
|
||
|
|
void Start () {
|
||
|
|
rigidbody.maxAngularVelocity = maxAngularVelocity;
|
||
|
|
contactSource = gameObject.AddComponent<AudioSource> ();
|
||
|
|
rollingSource = gameObject.AddComponent<AudioSource> ();
|
||
|
|
rollingSource.loop = true;
|
||
|
|
rollingSource.clip = rollingSound;
|
||
|
|
contactSource.volume = 0.4f;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
|
||
|
|
//Debug.Log(rigidbody.angularVelocity);
|
||
|
|
if (joy != null && rigidbody != null) {
|
||
|
|
|
||
|
|
float angle = Mathf.Rad2Deg * joy.Angle;
|
||
|
|
var forward = cam.transform.right;
|
||
|
|
forward.y = 0;
|
||
|
|
forward.Normalize();
|
||
|
|
|
||
|
|
|
||
|
|
Quaternion rotation = Quaternion.AngleAxis (angle, Vector3.up);
|
||
|
|
Vector3 speedVector = (rotation * forward);
|
||
|
|
speedVector = speedVector.normalized * (joy.Value * acceleration);
|
||
|
|
rigidbody.angularVelocity += Time.deltaTime * speedVector;
|
||
|
|
}
|
||
|
|
|
||
|
|
float minAng = 0.0f;
|
||
|
|
float maxAng = 3.0f;
|
||
|
|
|
||
|
|
rollingSource.volume = (Mathf.Clamp (rigidbody.angularVelocity.magnitude, minAng, maxAng) / (maxAng - minAng)) * 3.0f;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
bool grounded = false;
|
||
|
|
RaycastHit hit;
|
||
|
|
if (Physics.Raycast (this.transform.position - Vector3.up * collider.bounds.extents.y * 0.9f, -Vector3.up, out hit)) {
|
||
|
|
if (hit.distance < 0.1f) {
|
||
|
|
grounded = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if (rigidbody.angularVelocity.magnitude <= minAng || !grounded) {
|
||
|
|
|
||
|
|
if(rollingSource.isPlaying){
|
||
|
|
rollingSource.Stop ();
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if(!rollingSource.isPlaying && grounded){
|
||
|
|
rollingSource.Play();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnKill(GameObject killer){
|
||
|
|
manager.OnPlayerKilled ();
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnCollisionEnter(Collision collision) {
|
||
|
|
//foreach (ContactPoint contact in collision.contacts) {
|
||
|
|
// Debug.DrawRay(contact.point, contact.normal, Color.white);
|
||
|
|
//}
|
||
|
|
|
||
|
|
float maxMagnitude = 40.0f;
|
||
|
|
float minMagnitude = 4.0f;
|
||
|
|
audio.volume = Mathf.Clamp (collision.relativeVelocity.magnitude, minMagnitude, maxMagnitude) / (maxMagnitude - minMagnitude);
|
||
|
|
if (collision.relativeVelocity.magnitude > minMagnitude)
|
||
|
|
contactSource.PlayOneShot (floorContactSound);
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|