void FixedUpdate() { if (state == PlayerState.Playing && target) { float dist = Vector3.Distance(target.transform.position, transform.position); // Retrieve input. Note the use of GetAxisRaw (), which in this case helps responsiveness of the controls. // GetAxisRaw () bypasses Unity's builtin control smoothing. //Use the MovementSettings class to determine which drag constant should be used for the positional movement. //Remember the MovementSettings class is a helper class we defined ourselves. See the top of this script. rigidbody.drag = positionalMovement.ComputeDrag(Thruster, rigidbody.velocity); //Determines which direction the positional and rotational motion is occurring, and then modifies thrust/turn with the given accelerations. // Add torque and force to the rigidbody. Torque will rotate the body and force will move it. // Always modify your forces by Time.deltaTime in FixedUpdate (), so if you ever need to change your Time.fixedTime setting, // your setup won't break. //rigidbody.AddRelativeTorque(Vector3.up * Turn); if (dist > 50) { rigidbody.AddRelativeForce(Vector3.forward * Thruster * positionalMovement.positiveAcceleration); } } }
//FixedUpdate () is advantageous over Update () for working with rigidbody physics. void FixedUpdate() { if (GameData.Instance.Paused) { return; } // Retrieve input. Note the use of GetAxisRaw (), which in this case helps responsiveness of the controls. // GetAxisRaw () bypasses Unity's builtin control smoothing. thrust = Input.GetAxisRaw("Vertical"); turn = Input.GetAxisRaw("Horizontal"); if (!canControl) { thrust = 0.0f; turn = 0.0f; } //Use the MovementSettings class to determine which drag constant should be used for the positional movement. //Remember the MovementSettings class is a helper class we defined ourselves. See the top of this script. GetComponent <Rigidbody>().drag = positionalMovement.ComputeDrag(thrust, GetComponent <Rigidbody>().velocity); //Then determine which drag constant should be used for the angular movement. GetComponent <Rigidbody>().angularDrag = rotationalMovement.ComputeDrag(turn, GetComponent <Rigidbody>().angularVelocity); //Determines which direction the positional and rotational motion is occurring, and then modifies thrust/turn with the given accelerations. //If you are not familiar with the ?: conditional, it is basically shorthand for an "if..else" statement pair. See http://www.javascriptkit.com/jsref/conditionals.shtml thrust *= (thrust > 0.0) ? positionalMovement.positiveAcceleration : positionalMovement.negativeAcceleration; turn *= (turn > 0.0) ? rotationalMovement.positiveAcceleration : rotationalMovement.negativeAcceleration; // Add torque and force to the rigidbody. Torque will rotate the body and force will move it. // Always modify your forces by Time.deltaTime in FixedUpdate (), so if you ever need to change your Time.fixedTime setting, // your setup won't break. GetComponent <Rigidbody>().AddRelativeTorque(new Vector3(0.0f, 0.0f, -1.0f) * turn * Time.deltaTime, ForceMode.VelocityChange); GetComponent <Rigidbody>().AddRelativeForce(forwardDirection * thrust * Time.deltaTime, ForceMode.VelocityChange); }
void FixedUpdate() { if (state == PlayerState.Playing) { // Retrieve input. Note the use of GetAxisRaw (), which in this case helps responsiveness of the controls. // GetAxisRaw () bypasses Unity's builtin control smoothing. Thruster = LeftThruster + RightThruster; //Turn = LeftThruster - RightThruster; //Use the MovementSettings class to determine which drag constant should be used for the positional movement. //Remember the MovementSettings class is a helper class we defined ourselves. See the top of this script. rigidbody.drag = positionalMovement.ComputeDrag(Thruster, rigidbody.velocity); //Then determine which drag constant should be used for the angular movement. // rigidbody.angularDrag = rotationalMovement.ComputeDrag(Turn, rigidbody.angularVelocity); //Determines which direction the positional and rotational motion is occurring, and then modifies thrust/turn with the given accelerations. Thruster *= (Thruster > 0.0) ? positionalMovement.positiveAcceleration : positionalMovement.negativeAcceleration; //Turn *= (Turn > 0.0) ? rotationalMovement.positiveAcceleration : rotationalMovement.negativeAcceleration; // Add torque and force to the rigidbody. Torque will rotate the body and force will move it. // Always modify your forces by Time.deltaTime in FixedUpdate (), so if you ever need to change your Time.fixedTime setting, // your setup won't break. //rigidbody.AddRelativeTorque(Vector3.up * Turn); rigidbody.AddRelativeForce(Vector3.forward * Thruster, ForceMode.Acceleration); } }
/* All physics work should occur in FixedUpdate, as this is called during the physics frame. */ new void FixedUpdate() { base.FixedUpdate(); inputVal *= (inputVal > 0.0) ? rotationalMovement.positiveAcceleration : rotationalMovement.negativeAcceleration; GetComponent <Rigidbody>().angularDrag = rotationalMovement.ComputeDrag(inputVal, GetComponent <Rigidbody>().angularVelocity); GetComponent <Rigidbody>().AddRelativeTorque(rotationalMovement.forceNormal * inputVal * Time.deltaTime, ForceMode.VelocityChange); }
/* All physics work should occur in FixedUpdate, as this is called during the physics frame. */ override public void FixedUpdate() { base.FixedUpdate(); GetComponent <Rigidbody>().drag = positionalMovement.ComputeDrag(inputVal, GetComponent <Rigidbody>().velocity); lastDrag = GetComponent <Rigidbody>().drag; float forceMagnitude = (inputVal > 0.0) ? positionalMovement.positiveAcceleration : positionalMovement.negativeAcceleration; forceMagnitude *= inputVal; Vector3 force = positionalMovement.forceNormal * forceMagnitude; lastForce = force; GetComponent <Rigidbody>().AddRelativeForce(force, ForceMode.Acceleration); }