Execute
Save
Share
Share link
share
share
share
Team
Public Teams
Comments
0
Created By:
Guest
Title:
Title
Description
Edit
Copy Link
Login
Email *
Password *
Login
OR
Create Account
Screen Name *
Email *
Password *
Retype Password *
Team Access Code
Register
Public CodeBins
HELP
--Select Theme--
Ambiance
Blackboard
Cobalt
Eclipse
Elegant
Erlang-Dark
Lesser-Dark
Monokai
Neat
Night
Rubyblue
Vibrant-Ink
Xq-Dark
New CodeBin
CodeBins Versions
04/05/2015- V.1
Recent CodeBins
View All CodeBins
protected void rotateToFacePointer() { if (engineThruster == null) return; // Find direction of pointer vs ship, degrees Vector3 mousePosCoordinate = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector3 pdv = (mousePosCoordinate - ship.transform.position); pdv.Normalize(); // How many degrees we need to turn, > 0 means we need counterclockwise turn, < 0 means clockwise float angleToTurn = Mathf.DeltaAngle( Mathf.Atan2(ship.transform.right.y, ship.transform.right.x) * Mathf.Rad2Deg, Mathf.Atan2(pdv.y, pdv.x) * Mathf.Rad2Deg); // Our maximum available acceleration/sec float acceleration = engineThruster.thrustPower / ship.soRigidBody.mass; // The ship's absolute angular velocity, deg/sec float absAngVel = Mathf.Abs(ship.soRigidBody.angularVelocity); // The time we need to break to 0 angular velocity with max acceleration float timeToBreak = absAngVel / acceleration; // The distance in degrees that we cover while breaking to 0 deg/sec velocity float distanceTravelledWhileBreaking = ship.soRigidBody.angularVelocity * timeToBreak / 2; // Do we want to go counterclockwise? if (angleToTurn > 2.0f) { // Are we already rotating counter-clockwise? if (distanceTravelledWhileBreaking >= 0) { // Are we going too fast? If so, start breaking if (distanceTravelledWhileBreaking > angleToTurn) { foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_CLOCKWISE) b.runOnce(); } else { // We're not going too fast, so we can accelerate more to minimize turning time foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_COUNTERCLOCKWISE) b.runOnce(); } } else { // We are rotating clockwise, which is the wrong way, so break that rotation foreach(ComponentAction a in engineThruster.componentActions) { if (a.action == ActionType.ENG_ACCELERATE_COUNTERCLOCKWISE) a.runOnce(); } } } else if (angleToTurn < -2.0f) { // We want to go clockwise // Are we rotating counter-clockwise? If so, break it if (distanceTravelledWhileBreaking >= 0) { foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_CLOCKWISE) b.runOnce(); } else { // We are rotating clockwise // Are we going too fast? If so, start breaking(distanceTravelledWhileBreaking and angleToTurn are negative) if (distanceTravelledWhileBreaking < angleToTurn) { foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_COUNTERCLOCKWISE) b.runOnce(); } else { // We're not going too fast, so we can accelerate more to minimize turning time foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_CLOCKWISE) b.runOnce(); } } } else { // We're within stopping margin, so just break rotation entirely using a fraction of the available engine power // Our maximum available acceleration this update float accMax = acceleration * Time.deltaTime; float multiplier = absAngVel > acceleration ? 1.0f : absAngVel / acceleration; float backupEngUtil = engineThruster.utilization; engineThruster.utilization = multiplier == 0 ? 1.0f : multiplier; if (distanceTravelledWhileBreaking > 0) { foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_CLOCKWISE) b.runOnce(); } else if (distanceTravelledWhileBreaking < 0) { foreach(ComponentAction b in engineThruster.componentActions) if (b.action == ActionType.ENG_ACCELERATE_COUNTERCLOCKWISE) b.runOnce(); } engineThruster.utilization = backupEngUtil; } }
Bottom of Page
In Head
On Load
On Ready
Setting
Validate
Copy
Format
Setting
Validate
Copy
Format
No Doc Type
HTML5
HTML 4.01 Transitional
HTML 4.01 Strict
HTML 4.01 Frameset
XHTML 1.1
XHTML 1.0 Transitional
XHTML 1.0 Strict
XHTML 1.0 Frameset
Copy
Format
Download
×
Code Description
×
Difference of Versions
HTML
CSS
JS
×
JS Error
×
CSS Error
Errors
Warnings
×
JavaScript Setting
JS Libraries:
Chrome Frame 1.0.3
Dojo 1.8.0
Dojo 1.7.3
Dojo 1.7.2
Ext Core 3.1.0
jQuery 1.8.0
jQuery 1.7.2
jQuery 1.6.0
jQuery 1.5.0
jQuery 1.4.4
jQuery 1.4.0
jQuery-min 1.7.2
jQueryUI-min 1.8.21
MooTools more-1.4.0.1-full
MooTools core-1.4.5-full
MooTools core-1.4.1-full
Prototype 1.7.1.0
script.aculo.us 1.9.0
SWFObject 2.2
Twitter Bootstrap 2.0.4
WebFont Loader 1.0.28
yui 3.5.1
User Libraries:
Upload File
JavaScript URL(s):
×
CSS Setting
CSS Libraries:
jQueryUI 1.8.21
Twitter Bootstrap 2.0.4
User Libraries:
Upload File
CSS URL(s):