- Home /
Adjust turn rate by forward momentum (speed)
I'm trying to make a little sailing simulation. I'm not interested in dead-on realistic physics, just something that approximates the feeling. So far I've just got movement speed and turn rate based on mouse position:
var speedSensitivity = 1;
var rudderSensitivity = 1;
function Update () {
//Get the mouse position and convert it to a manageable number with 0,0 being the center of the screen
var mouseY = ((Input.mousePosition.y - (Screen.height / 2)) / Screen.height)*512;
var mouseX = ((Input.mousePosition.x - (Screen.width / 2)) / Screen.width)*32;
//Adjust the values to the sensitivity sliders
var forwardForce = mouseY * Time.deltaTime * speedSensitivity;
var rotationForce = mouseX * Time.deltaTime * rudderSensitivity;
//Add forwardForce to the forward direction of the rigidbody
rigidbody.AddRelativeForce(Vector3.forward * forwardForce);
//Turn by rotationForce
transform.Rotate(0,rotationForce,0);
}
This script works fine for my purposes, but I want to adjust the turn rate based on how fast the ship is moving forward—so at 0 speed, it doesn't turn at all, and at high speed, it turns a lot (emulating how a rudder works). I gather I need to multiply the rotationForce by some amount of either the absolute velocity or forward velocity of the rigidbody, but how do I detect those values?
P.S. Pretty much total newbie to both programming and Unity; please forgive me if I've missed something totally obvious! :)
Answer by rutter · Mar 17, 2012 at 01:08 AM
Ultimately, it looks like you want to multiply rotationForce by some number, for example by rigidbody.velocity.magnitude.
Perhaps something like this:
var x = rigidbody.velocity.magnitude * SOMENUMBER;
transform.Rotate(0,rotationForce*x,0);
You could set up some other mathematical relationships, and you'll probably want to tweak this until it feels right, but I assume that's the basic idea you're looking for?
That did exactly what I wanted it to! Thanks! For anyone curious, here is the final code (I converted my values to floats for more accuracy):
var speedSensitivity : float = 1;
var rudderSensitivity : float = 1;
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)*512;
var mouseX : float = ((Input.mousePosition.x - (Screen.width / 2)) / Screen.width)*32;
var forward$$anonymous$$omentum = rigidbody.velocity.magnitude * 0.1;
//Adjust the values to the sensitivity sliders
var forwardForce : float = mouseY * Time.deltaTime * speedSensitivity;
var rotationForce : float = mouseX * Time.deltaTime * rudderSensitivity * forward$$anonymous$$omentum;
rigidbody.AddRelativeForce(Vector3.forward * forwardForce);
transform.Rotate(0,rotationForce,0);
}
Your answer
Follow this Question
Related Questions
rigidbody.AddForce(transform.forward * 5000) 1 Answer
Trying to make a Projectile Rotate and Move Forward.. 1 Answer
look at movedirection 1 Answer
How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer
How to Tilt a Spaceship Based on Inertia/Acceleration Force Separate from Facing Direction? 2 Answers