I’ve attached my script in case I’m no good at explaining the issue I am having.
I have several variables that will play a role in my script. In order to know what these variables are, I have to run some methods. For instance, I need to run a method I call DragResistance in order to calculate what the variable maxResistance will be, which will be used in another function in the script.
It seems I am doing something out of sync, though. I get all kinds of error messages. I cannot convert a float to a void, etc. I’m sure any competent programmer will see my error right away. I have reviewed the basic tutorials but it looks to me like I am doing things the right way. I just can’t figure this out.
Any help is greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlightControl : MonoBehaviour {
//USER CONTROLS***************************
public float rollSpeed;
public float pitchSpeed;
public float yawSpeed;
//PHYSICS VARIABLES******************************************************
//The birds mass
public Rigidbody theBird;
//Upward and forward thrust generated by wing flaps
public float thrustPower;
public float upThrust;
public float forwardThrust;
//Air physics - Drag Resistance Applied Inverse to Velocity
public float radius; //Radius of the flight object -- Higher equals more drag
private float dragArea; //Area of the flight object -- in this case a sphere
float resistanceFactor; // Resistance per k/m^3. Try to keep below 1
float maxResistance; //Per cubic meter
float velocityFactor;
//Air Physics - Lift Force Inverse to Velocity
private float airSpeed;
public float requiredAirSpeedForFlight;
private float lift;
//***************************************************************************
void Start()
{
SetInitialReferences();
DragResistance ();
velocityFactor = FindVelocityFactor ();
resistanceFactor = FindResistanceFactor();
}
void FixedUpdate ()
{
LiftOff ();
DirectionalControl ();
}
void LateUpdate()
{
ApplyForce ();
}
void SetInitialReferences()
{
theBird = GetComponent<Rigidbody> ();
}
void DirectionalControl()
{
float h = Input.GetAxis ("Horizontal") * rollSpeed * Time.deltaTime;
float v = Input.GetAxis ("Vertical") * pitchSpeed * Time.deltaTime;
float y = Input.GetAxis ("Yaw") * yawSpeed * Time.deltaTime;
theBird.AddTorque (transform.forward * -h);
theBird.AddTorque (transform.right * v);
theBird.AddTorque (transform.up * y);
}
void LiftOff()
{
if (Input.GetButtonDown ("Jump"))
theBird.AddRelativeForce (0, upThrust, forwardThrust, ForceMode.Impulse);
}
void DragResistance()
{
//Get the constant area and maximum resistance
dragArea = Mathf.PI * Mathf.Pow (radius, 2);
maxResistance = dragArea * Math.Pow (resistanceFactor, 3);
}
void FindResistanceFactor()
{
direction = theBird.velocity.direction; //Birds direction
angle = Vector3.Angle (transform.forward, direction); //Angle between bird direction and velocity direction
return Mathf.Abs (Mathf.Sin (angle)); //Return the resistance factor
}
void FindVelocityFactor ()
{
return theBird.velocity.magnitude; //Gets and returns the birds velocity magnitude
}
void ApplyForce()
{
float magnitude = maxResistance * resistanceFactor * velocityFactor; //Magnitude of air resistance
Vector3 direction = transform.forward.normalized * -1; //calculate the direction
theBird.AddRelativeForce (direction*magnitude); //Add the force to the rigidbody
}
void LiftEquation()
{
if (airSpeed >= requiredAirSpeedForFlight)
{
theBird.AddRelativeForce (0, lift, 0);
}
}
void DefineLift()
{
}
void FindAirSpeed()
{
}
}