- Home /
Programming boat sails to turn according to wind direction
I am trying to make a sailboat driven by the player with sails that turn as the boat moves according to the wind direction. I have used Boat Controller by NSdesign Games and got a very effective system to drive a Yacht I modeled in C4D. However my original plan to use Physics to make the sails turn has gone west because adding any physics colliders to the boat or its sail or boom mess up the Boat Controller floatation bit and the boat sinks. I don't think I can alter this in Boat Controller.
So, I am wondering how to go about a simple script that would turn the sail and boom on the boat within a limited amount so that the sail is always angled a little away from the wind direction like a real sail would, no matter how the boat is steered the sails turn to align with the wind direction.
I have not found a script example yet that used a constant direction to turn from, only fixed co ordinates, and am wondering if anyone has any ideas how to go about this.
There's a wheel to turn too, but I think that should be straightforward, its the turning according to a specific world direction thing I am stuck on so far.
Thank you.
Hi,
Thank you. I will look at that. I can't use any physics that involves attaching colliders to any part of the boat without messing up the Boat Controller script. This is the part of the Boat Controller script that seems to deal with the steering:
// Update is called once per frame
void FixedUpdate (){
//If there is no water surface we are colliding with, no boat physics
if(waterSurface==null)
return;
float motor = 0.0f;
float steer = 0.0f;
if(canControl){
motor = Input.GetAxis("Vertical");
steer = Input.GetAxis("Horizontal");
}
I think, maybe, what I need to do is make about four or five animations and have them called by a script depending on which way the boat is facing. So the script would need to know which way the boat was facing. Its tricky.
The mast and sail boom are easy. Its just rotating an object. Just keep track of where you are with relation to the x world axis.
Consider making a BlendShape animation for the billow of the sail in the wind.
Anyhoo, sails aren't designed to move in the wind :D They are meant to be moved to use the wind :P
Is this a boat-smashing-against-cliff simulator?
Well it would be WAY more cool if I could program the boat to use the wind direction and get all the trees and plants to do the same rather than relying on a driving script with a fake sail animation but that is well past my abilities!
No smashing planned, it just bumps away from things you drive it into. Its a boat sailing round islands looking for things affair.
Thank you meat5000, I vacillate between trying to call an animation and do it by scripting rotation along the lines you suggest. Either way has its complexities. I'll look into the Blendshape thing too. Thanks!
There is a "Wind Zone" component! And a Constant Force, but thats not so useful to you.
Essentially, use Vector Dot Product to deter$$anonymous$$e how aligned the sail is with Wind direction and use that to move your Blendshape slider from 0->1 to billow your sail out further.
The calculations are pretty simple and akin to Gravity or Camera relative control in some ways.
Answer by robertbu · Nov 19, 2014 at 11:33 PM
This is a pretty open ended questions in the absence of some code, but I think Quaternion.LookRotation() will do the job for you. Given that you've calculated the direction you want to point the boat, you can do this in an Update() loop:
transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(dir), speed * Time.deltaTime);
'speed' is a variable you create and assign and is measured in degrees per second. 'dir' the direction you want the boat to face. If you'd like a more eased rotation, you can replace 'RotateTowards' with 'Slerp' and greatly reduce the value of 'speed'. I cannot tell from this question if any of the Physics engine is being used. If Physics is being used, consider using Physics.MoveRotation() rather than directly assigning the rotation as I've done in this line of code.
Thank you robertbu, I shall look at that. I've several ideas on how to go about this project, not sure any of them will work, but Quaternion.LookRotation looks very interesting and I shall research it. Thanks
Answer by Jeff-Kesselman · Nov 25, 2014 at 03:56 AM
when you sail a boat, you don't let the sails go where the wind pushes them. Keeping them tight against the wind is actually what gets you your best speed.
Unless its a spinnaker, a sail ISNT a parachute... its a wing.
I know, I used to sail, but can't write scripts, unless they are REALLY basic, and can't figure out how to make the boat sail properly, IE, only if you get the sail and the boat at the correct angle to the wind.
Have majorly fudged it with Boat Controller, which is meant for a motor boat, and physics after all. But it's not right.
Also I can't get a spinnaker or genoa to work as cloth physics doesn't do two sided meshes so you either get a sail that is invisible from one side or a sail that has really weird artifacts on both which is why the mainsail is really flat!
I think its do-able with animation and then I was going to use a bunch of "if" statements to call the animations and boat speed depending on the boats angle to the world space wind, but I got bogged down with the scripting again and how to get info from world space on which way the boat is facing in x, y, z, which you'd think would be standard but I can't find examples to learn from, so its on the list for rather later when I'm further along with the whole project.
Why is it so hard to get the x, y, z of a GameObjects orientation in world space to feed into a script to work from? I figured there'd be a load of Enemy type scripts knocking about I could learn from but can't find anything relevant to Game Object orientation in 3D space feeding info back. $$anonymous$$aybe I'm using the wrong search terms.
The hopefully temporary bodge: http://hayesdavies.co.uk//Boat%20Test1.html
It's an abbreviated Shipman 63 I modeled up quickly and the Boat Controller script from the Asset Store.
Your answer
Follow this Question
Related Questions
Trouble with Turning Animation 2 Answers
my animation script does not work 0 Answers
Initiate Animation 0 Answers
Whats wrong with this ladder climb script? 1 Answer
Animation after 5 shots 1 Answer