Hi there,
I’m new to unity development so I bet I’m missing something simple.
I have a vehicle/rigidbody (boat) that sits on top of a collider (water). I’m trying to override the collision and place my object partly below the water. The issues is OnTriggerStay is never called when the boat collides w/ the water. Am I missing something?
using UnityEngine;
using System.Collections;
public class JetSkiController : MonoBehaviour {
public Transform water = null;
public Transform propeller = null;
private float thrust = 0;
private float steering = 0;
private Rigidbody rigidBody;
private bool inputEnabled = true;
void Start () {
if(rigidBody) {
Destroy(rigidbody);
}
rigidBody = gameObject.AddComponent("Rigidbody") as Rigidbody;
rigidbody.mass = .5f;
rigidBody.drag = 1;
rigidbody.angularDrag = 1;
rigidbody.useGravity = true;
rigidbody.interpolation = RigidbodyInterpolation.Interpolate;
}
void FixedUpdate() {
HandleFloating();
HandleThrust();
HandleSteering();
}
void HandleFloating() {
//Debug.Log(transform.position.y);
float waterLevel = water.position.y;
/*
float waterLevel = water.collider.bounds.max.y;
float distanceFromWaterLevel = transform.position.y - waterLevel;
//float percentUnderWater = Mathf.Clamp01(( - distanceFromWaterLevel + 0.5 * size.y) / size.y);
Debug.Log("waterY: " + waterLevel);
//Debug.Log("boatY: " + rigidBody.position.y);
if(water.transform.position.y > rigidBody.position.y) {
}
*/
}
void HandleThrust() {
float thrustInput = Input.GetAxis("Vertical");
if (thrustInput > 0)
thrust += 1;
else if (thrustInput < 0)
thrust -= 1;
else if (thrustInput == 0);
thrust *= 0.9f;
rigidBody.AddRelativeForce(Vector3.forward * thrust);
}
void HandleSteering() {
float steeringInput = Input.GetAxis("Horizontal");
float friction = 0.47f;
float increment = thrust * .005f;
float max = 2f;
if (steeringInput > 0)
steering += (steering > max) ? 0 : increment;
else if (steeringInput < 0)
steering -= (steering > max) ? 0 : increment;
else if (steeringInput == 0)
steering *= friction;
rigidBody.AddRelativeTorque(0, steering, 0);
rigidBody.AddRelativeTorque(0, 0, steering * .1f );
}
void OnTriggerStay(Collider other) {
Debug.Log("collision");
// if (other.GetComponent<OtherScript>())
// other.GetComponent<OtherScript>().DoSomething();
}
}