Last active
February 26, 2019 15:51
-
-
Save danopia/b0f42dde2e33e7974c2e396a7f0eccd2 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; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
using Random = UnityEngine.Random; | |
[RequireComponent(typeof(Rigidbody))] | |
public class PhysicalGolfball : MonoBehaviour | |
{ | |
[SerializeField] | |
private AudioSource _audioSource; | |
[SerializeField] | |
private List<AudioClip> _impactSounds; | |
[SerializeField] | |
private AudioClip _jumpSound; | |
[SerializeField] | |
private AudioClip _fallSound; | |
[SerializeField] | |
private AudioClip _failSound; | |
[SerializeField] | |
private AudioClip _holeSound; | |
[SerializeField] | |
private AudioClip _clapSound; | |
[SerializeField] | |
private float _controlForce = 10f; | |
[SerializeField] | |
private bool _kickStartForce; | |
[SerializeField] | |
private PhysicMaterial _holeMaterial; | |
[SerializeField] | |
private Camera _mainCamera; | |
[SerializeField] | |
private bool _adjustCamForIso; | |
[SerializeField] | |
private float _camFollowDistance = 8f; | |
[SerializeField] | |
private float _camIsoOffset= 0f; | |
private Rigidbody _rigidbody; | |
private Vector3 _lastCollisionPoint; | |
private bool _inHole = false; | |
private float _smallestZ = 0f; | |
private void Start() | |
{ | |
_rigidbody = GetComponent<Rigidbody>(); | |
_lastCollisionPoint = _rigidbody.position; | |
_smallestZ = _mainCamera.transform.position.z; | |
if (_kickStartForce) | |
{ | |
_rigidbody.AddForce(Vector3.right * _controlForce * 2f); | |
} | |
} | |
private IEnumerator OnCollisionEnter(Collision c) | |
{ | |
if (_inHole) yield return null; | |
if (c.collider.sharedMaterial == _holeMaterial) | |
{ | |
_inHole = true; | |
_rigidbody.velocity = Vector3.zero; | |
_audioSource.PlayOneShot(_holeSound); | |
yield return new WaitForSeconds(0.5f); | |
_audioSource.PlayOneShot(_clapSound); | |
yield return new WaitForSeconds(6f); | |
int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1; | |
if (SceneManager.sceneCountInBuildSettings > nextSceneIndex) | |
{ | |
SceneManager.LoadScene(nextSceneIndex); | |
} | |
else | |
{ | |
SceneManager.LoadScene(0); | |
} | |
} | |
else | |
{ | |
int soundIdx = Random.Range(0, _impactSounds.Count - 1); | |
_audioSource.PlayOneShot(_impactSounds[soundIdx], Mathf.Min(_rigidbody.velocity.magnitude / 5f, 1)); | |
} | |
} | |
private void OnCollisionExit(Collision other) | |
{ | |
_lastCollisionPoint = _rigidbody.position; | |
} | |
private void FixedUpdate() | |
{ | |
// loosely follow ball with camera | |
var camPos = _mainCamera.transform.position; | |
var desiredX = _rigidbody.position.x; | |
var desiredZ = Math.Max(_smallestZ, _rigidbody.position.z - _camFollowDistance); | |
if (_adjustCamForIso) | |
{ | |
var diff = (_smallestZ) - camPos.z; | |
desiredX += diff * 0.016f - _camIsoOffset; | |
} | |
float camX = ((camPos.x * 14f) + desiredX) / 15f; | |
float camZ = ((camPos.z * 14f) + desiredZ) / 15f; | |
_mainCamera.transform.position = new Vector3(camX, camPos.y, camZ); | |
// no interaction when won | |
if (_inHole) return; | |
// jump | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
_audioSource.PlayOneShot(_jumpSound); | |
_rigidbody.AddForce(Vector3.up * _controlForce * 25f); | |
} | |
// WASD | |
if (Input.GetKey(KeyCode.W)) | |
{ | |
_rigidbody.AddForce(Vector3.forward * _controlForce); | |
} | |
else if (Input.GetKey(KeyCode.S)) | |
{ | |
_rigidbody.AddForce(Vector3.back * _controlForce); | |
} | |
if (Input.GetKey(KeyCode.A)) | |
{ | |
_rigidbody.AddForce(Vector3.left * _controlForce); | |
} | |
else if (Input.GetKey(KeyCode.D)) | |
{ | |
_rigidbody.AddForce(Vector3.right * _controlForce); | |
} | |
// falling off | |
if (_rigidbody.position.y < 70) | |
{ | |
_audioSource.PlayOneShot(_fallSound); | |
_audioSource.PlayOneShot(_failSound); | |
_rigidbody.velocity = Vector3.up * 2; | |
_rigidbody.position = new Vector3( | |
_lastCollisionPoint.x, | |
Mathf.Max(90, _lastCollisionPoint.y), | |
_lastCollisionPoint.z); | |
// reset orth changes | |
if (_adjustCamForIso) | |
{ | |
_mainCamera.orthographicSize = 2f; | |
} | |
} else if (_rigidbody.position.y < 85) | |
{ | |
// zoom out when falling | |
if (_adjustCamForIso) | |
{ | |
_mainCamera.orthographicSize = Mathf.Min(_mainCamera.orthographicSize + 0.1f, 5); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment