using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class BoatController : MonoBehaviour { private Rigidbody rigidBody; [SerializeField] private float ForwardFactor = 1f; [SerializeField] private float RotationFactor = 1f; [SerializeField] private float BrakeFactor = 1f; [SerializeField] private float ObstacleBounce = 2f; [SerializeField] private bool m_usedebugkeys; [SerializeField] private AudioClip[] m_bonkClips; void Awake() { rigidBody = GetComponent(); } // Update is called once per frame void Update() { if (!m_usedebugkeys) return; //get input if (Input.GetKeyDown(KeyCode.A)) { RowLeft(); } if (Input.GetKeyDown(KeyCode.D)) { RowRight(); } if (Input.GetKeyDown(KeyCode.S)) { Brake(); } } private void FixedUpdate() { //print(transform.rotation.eulerAngles.y); //constrain rotation if (transform.rotation.eulerAngles.y > 180 && transform.rotation.eulerAngles.y < 270) { print("under"); transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f)); rigidBody.angularVelocity = Vector3.zero; } //constrain rotation if (transform.rotation.eulerAngles.y < 180 && transform.rotation.eulerAngles.y > 90f) { print("over"); transform.rotation = Quaternion.Euler(new Vector3(0f, 90f, 0f)); rigidBody.angularVelocity = Vector3.zero; } } public void RowLeft() { rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration); rigidBody.AddTorque(transform.up * -RotationFactor); } public void RowLeft(float velocity) { rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration); rigidBody.AddTorque(transform.up * -RotationFactor * velocity); } public void RowRight() { rigidBody.AddForce(transform.forward * ForwardFactor, ForceMode.Acceleration); rigidBody.AddTorque(transform.up * RotationFactor); } public void RowRight(float velocity) { rigidBody.AddForce(transform.forward * ForwardFactor * velocity, ForceMode.Acceleration); rigidBody.AddTorque(transform.up * RotationFactor * velocity); } public void Brake() { rigidBody.AddForce(rigidBody.velocity * - BrakeFactor); rigidBody.AddTorque(rigidBody.angularVelocity * -BrakeFactor); } public void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Obstacle")) { Debug.Log($"Hitting: {collision.gameObject} Foce: {collision.impulse * ObstacleBounce}"); rigidBody.AddForce(collision.impulse * ObstacleBounce, ForceMode.Impulse); SFXPlayer.Play(m_bonkClips); } } }