void FixedUpdate() { if (target) { seekPos = target.position; } CalculateImmediateTarget(); Vector3 force = pid.Update(targetPos, transform.position, Time.deltaTime); force = Vector3.ClampMagnitude(force, maxForce); //if (rb.velocity.magnitude < maxSpeed) //rb.AddForce(transform.up * -Physics.gravity.y + force); //rb.AddRelativeForce(Vector3.up * -Physics.gravity.y + force); if (rb.velocity.magnitude > 5) { force = Vector3.zero; } totalForce = transform.up * -Physics.gravity.y + force; rb.AddForce(totalForce); }
public void FixedUpdate() { if (Manager.Instance.gameWon) { return; } // Heading to target logic. var angularVelocityError = rigidbody.angularVelocity * -1; Debug.DrawRay(transform.position, rigidbody.angularVelocity * 10, Color.black); var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); Debug.DrawRay(transform.position, angularVelocityCorrection, Color.green); rigidbody.AddTorque(angularVelocityCorrection); var desiredHeading = target.position - transform.position; Debug.DrawRay(transform.position, desiredHeading, Color.magenta); var currentHeading = transform.forward; Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); rigidbody.AddTorque(headingCorrection * (flightSettings.RollSpeed / 100)); // flying forward logic. GetComponent <Rigidbody>().AddForce(transform.forward * flightSettings.BasicThrustSpeed * Time.fixedDeltaTime); }
public void FixedUpdate() { /* * var angularVelocityError = rb.angularVelocity * -1; * Debug.DrawRay(transform.position, rb.angularVelocity * 10, Color.black); * * var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); * Debug.DrawRay(transform.position, angularVelocityCorrection, Color.green); * * rb.AddTorque(angularVelocityCorrection);*/ //Vector3 upTGT; var desiredHeading = Vector3.up; Debug.DrawRay(transform.position, desiredHeading, Color.magenta); var currentHeading = transform.up; Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); rb.AddTorque(headingCorrection); }
//======================================================================== // SAMPLE BEHAVIOURS //======================================================================== void RunAway() { Rigidbody rigidBody = GetComponent <Rigidbody>(); Collider playerCollider = null; //don't run away if we have him in our sites!!! if ((myState.distToPlayer <= 25 && !myState.playerSpotted) || Time.time - 0.75f < hitTime) { //change direction away from player var angularVelocityError = rigidBody.angularVelocity * -1; var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, 0.1f); angularVelocityCorrection = angularVelocityError * 40; myCommands.angularCorrection = angularVelocityCorrection; var headingError = Vector3.Cross(transform.up * -1.0f, myState.desiredHeading * -1.0f); var headingCorrection = headingController.Update(headingError, 0.1f); headingCorrection = headingError.normalized * 40.0f; myCommands.torque = headingCorrection; //go faster myCommands.thrust = speed * 1.5f; finCycleTime = 0.05f; var player = GameObject.Find("PlayerShip"); if (player != null) { playerCollider = player.GetComponent <Collider>(); if (playerCollider != null) { var bounds = playerCollider.bounds; //raycast on target var lineToTarget = new Ray(transform.position, transform.up * -1.0f); if (!bounds.IntersectRay(lineToTarget)) { myCommands.thrust = speed * 2.0f; } } } } }
void turnTowardsWorldPosition(Vector3 target) { var angularVelocityError = rb.angularVelocity * -1; //Debug.DrawRay(transform.position, rb.angularVelocity * 10, Color.black); var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); //Debug.DrawRay(transform.position, angularVelocityCorrection, Color.green); rb.AddTorque(angularVelocityCorrection); var desiredHeading = target - transform.position; //Debug.DrawRay(transform.position, desiredHeading, Color.magenta); var currentHeading = transform.forward; //Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); rb.AddTorque(headingCorrection); }
//======================================================================== // SAMPLE BEHAVIOURS //======================================================================== void RunAway() { Rigidbody rigidBody = GetComponent <Rigidbody>(); Collider playerCollider = null; if (myState.distToPlayer <= 25 && !myState.playerSpotted || myState.distToPlayer <= 10) { //change direction away from player var angularVelocityError = rigidBody.angularVelocity * -1; var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, 0.1f); angularVelocityCorrection = angularVelocityError * 40; myCommands.angularCorrection = angularVelocityCorrection; var headingError = Vector3.Cross(transform.forward, myState.desiredHeading * -1.0f); var headingCorrection = headingController.Update(headingError, 0.1f); headingCorrection = headingError.normalized * 40; myCommands.torque = headingCorrection; //run away fast myCommands.thrust = speed * 1.5f; //faster if he's right behind us var player = GameObject.Find("PlayerShip"); if (player != null) { playerCollider = player.GetComponent <Collider>(); var bounds = playerCollider.bounds; //raycast on target var lineToTarget = new Ray(transform.position, transform.forward); if (!bounds.IntersectRay(lineToTarget)) { myCommands.thrust = speed * 2.0f; } } } }
void steerToTarget(Vector3 desiredHeading) { currentHeading = transform.forward; Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); Debug.DrawRay(transform.position, desiredHeading, Color.magenta); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); rb.AddTorque(headingCorrection); var angularVelocityError = rb.angularVelocity * -1; var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); rb.AddTorque(angularVelocityCorrection); }
public void Rotation() { Vector3 rotateVector = new Vector3(CrossPlatformInputManager.GetAxisRaw("RotateHorizontal"), CrossPlatformInputManager.GetAxisRaw("RotateVertical"), 0); targetAngle = (Mathf.Atan2(rotateVector.y, rotateVector.x) * Mathf.Rad2Deg - 90); float angleError = Mathf.DeltaAngle(transform.eulerAngles.z, targetAngle); float torqueCorrectionForAngle = angleController.Update(angleError, Time.fixedDeltaTime); rigidB.angularVelocity = Mathf.Clamp(rigidB.angularVelocity, -maxAngularVelocity, maxAngularVelocity); float angularVelocityError = -rigidB.angularVelocity; float torqueCorrectionForAngularVelocity = angularVelocityController.Update(angularVelocityError, Time.fixedDeltaTime); torque = torqueCorrectionForAngle + torqueCorrectionForAngularVelocity; rigidB.AddTorque(torque); }
public void AttackPlayer() { if (Time.time > timer && startAttacking) { float targetAngle = (Mathf.Atan2(moveVector.y, moveVector.x) * Mathf.Rad2Deg - 90); float angleError = Mathf.DeltaAngle(transform.eulerAngles.z, targetAngle); float torqueCorrectionForAngle = angleController.Update(angleError, Time.fixedDeltaTime); rigidB.angularVelocity = Mathf.Clamp(rigidB.angularVelocity, -MaxAngularVelocity, MaxAngularVelocity); float torque = torqueCorrectionForAngle; int changeDir = 1; if (Mathf.Abs(rigidB.angularVelocity) < 5 && torque > 0) { changeDir = -4; } else if (Mathf.Abs(rigidB.angularVelocity) < 5 && torque < 0) { changeDir = 4; } if (rigidB.angularVelocity < 0 && torque > 0) { changeDir = -1; } else if (rigidB.angularVelocity > 0 && torque < 0) { changeDir = -1; } torque *= changeDir; rigidB.AddTorque(torque); timer = Time.time + AttackSpeed; } Quaternion rotation = Quaternion.Euler(0, rotationY + 16, 0); rigidB.AddForce((playerTransform.position - (rotation * moveVector)) * Time.fixedDeltaTime); }
public void FixedUpdate() { if (target != null) { angularVelocityError = rigidBody.angularVelocity * -1; Debug.DrawRay(transform.position, rigidBody.angularVelocity * 10, Color.black); angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); Debug.DrawRay(transform.position, angularVelocityCorrection, Color.green); rigidBody.AddTorque(angularVelocityCorrection); desiredHeading = target.position - transform.position; Debug.DrawRay(transform.position, desiredHeading, Color.magenta); currentHeading = transform.up; Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); headingError = Vector3.Cross(currentHeading, desiredHeading); headingCorrection = headingController.Update(headingError, Time.deltaTime); rigidBody.AddTorque(headingCorrection); } }
void turnTowardsHeading(Vector3 desiredHeading, Vector3?desiredUp = null) { if (desiredUp == null) { desiredUp = Vector3.up; } // local y points up, up control should happen as rotations about x and z axes var angularVelocityError = rb.angularVelocity * -1; //Debug.Log (angularVelocityError); //Debug.DrawRay(transform.position, rb.angularVelocity * 10, Color.black); var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); Debug.DrawRay(transform.position, angularVelocityCorrection, Color.red); rb.AddTorque(angularVelocityCorrection); //Debug.DrawRay(transform.position, desiredHeading, Color.magenta); var currentHeading = transform.forward; //Debug.DrawRay(transform.position, currentHeading * 15, Color.blue); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); Debug.DrawRay(transform.position, headingCorrection, Color.green); rb.AddTorque(headingCorrection); var currentUp = transform.up; var upError = Vector3.Cross(currentUp, desiredUp.Value); var upCorrection = upController.Update(upError, Time.deltaTime); Debug.DrawRay(transform.position, upCorrection, Color.blue); rb.AddTorque(upCorrection); }
// Update is called once per frame void FixedUpdate() { if (Level.AllowMotion) { /** calc closest poiunt on path */ if (cPP_i+1 < pc.getPathPointCount()-1) { current_distance = Vector3.Distance(pc.getPathPoint(cPP_i), this.transform.position); next_distance = Vector3.Distance(pc.getPathPoint(cPP_i+1), this.transform.position); if (current_distance > next_distance) { cPP_i++; if (!pc.getPathObject(cPP_i).tag.Equals(prev_tag)) { Debug.Log(pc.getPathObject(cPP_i).tag); } prev_tag = pc.getPathObject(cPP_i).tag; } } /** push ship forward along the path */ if ((cPP_i + 1 < pc.getPathPointCount() - 1) && (logic.countdownOver)) { Vector3 forward = (pc.getPathPoint(cPP_i + 1) - pc.getPathPoint(cPP_i)).normalized * 4000; this.GetComponent<Rigidbody>().AddForce(forward); } /** rotate ship towards current path_point+40 */ if ((cPP_i + 40 < pc.getPathPointCount() - 1) && (logic.countdownOver)) { // pid controller to adjust the torgue of the ship // ref: http://webber.physik.uni-freiburg.de/~hon/vorlss02/Literatur/Ingenieurswiss/pid/pid+matlab/PID%20systems%20tutorial.htm VectorPid angularVelocityController = new VectorPid(1.7766f, 0, 0.2553191f); VectorPid headingController = new VectorPid(1.244681f, 0, 0.06382979f); Vector3 target = pc.getPathPoint(cPP_i + 40); var angularVelocityError = this.GetComponent<Rigidbody>().angularVelocity * -1; Debug.DrawRay(this.transform.position, this.GetComponent<Rigidbody>().angularVelocity * 10, Color.black); var angularVelocityCorrection = angularVelocityController.Update(angularVelocityError, Time.deltaTime); Debug.DrawRay(this.transform.position, angularVelocityCorrection, Color.green); this.GetComponent<Rigidbody>().AddTorque(angularVelocityCorrection); var desiredHeading = target - this.transform.position; Debug.DrawRay(this.transform.position, desiredHeading, Color.yellow); var currentHeading = this.transform.forward; Debug.DrawRay(this.transform.position, currentHeading * 15, Color.blue); var headingError = Vector3.Cross(currentHeading, desiredHeading); var headingCorrection = headingController.Update(headingError, Time.deltaTime); this.GetComponent<Rigidbody>().AddTorque(headingCorrection); } /** drag ship to its connected point*/ // apply force float perc_dist = current_distance / radius; // is the ship in the inner 60% around the actual portalpoint? if (perc_dist > 0.6f) { // calc the impact 0 - 100% of the force that should be applied float impact_factor = (perc_dist - 0.6f) / 0.4f; // 0 - 0.4 Vector3 push_f = ((pc.getPathPoint(cPP_i) - this.transform.position).normalized * 22500) * impact_factor; this.GetComponent<Rigidbody>().AddForce(push_f); } /** limit velocity of ship */ float current_vel = fastEuclidDist(transform.position, prev_pos); if (current_vel > 3.6f) { // too fast > reduce maxspeed spbar.setSpeed(100); this.GetComponent<Rigidbody>().velocity = this.GetComponent<Rigidbody>().velocity * 0.99f; } else { if (current_vel > 3.5f) { spbar.setSpeed(100); } else { //Debug.Log("current spedd" + (current_vel / 3.6f)); int speed = Mathf.RoundToInt((current_vel / 3.6f) * 100); spbar.setSpeed(speed); } } prev_pos = transform.position; } }