NullReferenceException & Math Quandary

Hi there! Two unrelated questions in the same code. Just to explain, I am trying to make a little sailing simulator. Nothing fancy, just basically moving the boat forward depending on how aligned the sail and hull angle are to the wind angle.

(1) I know my equation for determining forwardForce is wrong; I’ve fiddled with a few variations, but I’m kind of lost. Any pointers would be much appreciated! (note: windAngle is arbitrary).

(2) Unrelated to my first issue, I get a “NullReferenceException: Object reference not set to an instance” when I run the game; this is because it seems to be unable to find theBoat, despite the fact that I’ve made sure to link it to my boat GameObject by dragging it into the appropriate box in the inspector. I tried searching, but couldn’t make heads or tails of the solutions provided to others. What am I missing?

var yMouseSensitivity : float = 1;
var speedSensitivity : float = 1;
var sailSensitivity : float = 1;
var rudderSensitivity : float = 1;
var windAngle : float = 90;
public var theBoat : GameObject;

function Update () {

//Get the mouse position and convert it to a manageable number with 0,0 being the center of the screen
var mouseY : float = ((Input.mousePosition.y - (Screen.height / 2)) / Screen.height)*120;
var mouseX : float = ((Input.mousePosition.x - (Screen.width / 2)) / Screen.width)*32;

//Turn rate dependent on forward momentum of rigidbody
var forwardMomentum = rigidbody.velocity.magnitude * 0.1;

//Adjust the values to the sensitivity sliders
var sailAngle : float = mouseY*yMouseSensitivity;
var rotationForce : float = mouseX * Time.deltaTime * rudderSensitivity * forwardMomentum;

var hullAngle : float = theBoat.transform.EulerAngles.y;

var forwardForce = 1-((windAngle/360)-((hullAngle+sailAngle)/360))*speedSensitivity;

rigidbody.AddRelativeForce(Vector3.forward*forwardForce);
transform.Rotate(0,rotationForce,0);
}

Thanks in advance! :slight_smile:

Lower-case EulerAngles? (see the refernce) I’m thinking javascript hates to complina about unknown vars, but then .y stumps it.

For force, I think you want dot-products: dot(windDir, sailDir) gives you the percent of wind force at the wind to sail angle. The dot(sailDir, hullDir) does the same thing for sail to hull. The DIRS would be the forward vectors.