Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by maouna · Mar 24, 2018 at 05:36 PM · development

way to make boat rotate and move at the same time with a rigidbody and addforce ?

hi i have boat and i want any way to make boat rotate and move at the same time with a rigidbody and addforce how can i change ur code

thanks a lot

using UnityEngine;

public class BlockMovement : MonoBehaviour { public GameObject cube; public float speed = 50f; public float rotationSpeed = 50f; public float minRotation = -45f; public float maxRotation = 45f;

  void Update()
  {
      float deltaTime = Time.deltaTime;
 
      float horizontalInput = Input.GetAxis("Horizontal");
      if (horizontalInput != 0f)
      {
          float deltaMovement = horizontalInput * speed * deltaTime;
          cube.transform.Translate(new Vector3(0f, 0f, deltaMovement));
      }
 
      bool isTurningLeft = Input.GetKey(KeyCode.Q);
      bool isTurningRight = Input.GetKey(KeyCode.E);
      if (isTurningLeft && !isTurningRight)
      {
          cube.transform.Rotate(Vector3.left * rotationSpeed * deltaTime);
      }
 
      else if (!isTurningLeft && isTurningRight)
      {
          cube.transform.Rotate(Vector3.right * rotationSpeed * deltaTime);
      }
  }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Honorsoft · Apr 01, 2018 at 10:45 PM

I just started trying out boat-Physics in Unity, and am finding only complicated (physics-based) methods shown online. Although those are great for boat-simulators that need realistic calculations, I wanted something simpler (quicker and less processor-intensive too) so I ended up building a basic boat-system, one that so far only uses 6 lines of code (2 per script) to control, sway and rock the boat. So here's a simpler (and fairly realistic looking) 'boat effect' to simulate the 'bobbing' and 'swaying' of a ship at sea: Here's three simple scripts, one (boatRock.cs) adds a rocking motion the length of the ship, and the other (bostSway.cs) adds a lesser 'horizontal twisting', and boatSail moves the boat forward at a set speed. Put the 3 scripts together on your BOAT object, and the values can be altered from script according to the water conditions (e.g., a storm). Then set the Main Camera as a child of the BOAT so it follows the BOAT. Here's the 3 scripts, and also I included a starter 'pirate scene' to see how it is set up, and to help anyone learning Unity. I have tried the Mesh-modifier methods and some hinge-joint methods, but dealing with rigidbodies and colliders an be glitchy in Unity, and I got great results so far so I thought I'd post them. One note, I didn't set up any mesh-modifier or mesh-filter yet to make the waves, I am still trying to find a simpler method of making waves in Unity.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 /*
 * If you want to slow down or speed up the oscillation, just multiply Time.time by a constant.
 * Multiplying Time.time by a constant greater than 1 will speed up the oscillation.
 * Multiplying Time.time by a constant less than 1 will slow down the oscillation.
 */
 
 public class boatRock : MonoBehaviour {
 public float floor = -2.0f;
 public float ceiling = 2.0f;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     float tilt = floor + Mathf.PingPong (Time.time, ceiling - floor);
     
     transform.rotation = Quaternion.AngleAxis(tilt*Time.deltaTime, Vector3.left) * transform.rotation;
     }
 
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 /*
 * If you want to slow down or speed up the oscillation, just multiply Time.time by a constant.
 * Multiplying Time.time by a constant greater than 1 will speed up the oscillation.
 * Multiplying Time.time by a constant less than 1 will slow down the oscillation.
 */
 
 public class boatSway : MonoBehaviour {
 public float floor = -1.0f;
 public float ceiling = 1.0f;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     float tilt = floor + Mathf.PingPong (Time.time, ceiling - floor);
     
     transform.rotation = Quaternion.AngleAxis(tilt*Time.deltaTime, Vector3.up) * transform.rotation;
     }
 }
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class boatSail : MonoBehaviour {
 
     public float bSpeed = 1.5f;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         transform.position += transform.forward * bSpeed * Time.deltaTime;
     }
 }

Here's the example project with a ship scene to get anyone started: http://www.mediafire.com/file/bd2pohbb794d461/BoatSceneExample1.unitypackage

alt text


examplescene01.jpg (209.7 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Honorsoft · Apr 01, 2018 at 10:58 PM 0
Share

This method was set up as simple as possible for learners, but could be easily adapted to use in a game. It also shows some simple artificial-shadow techniques (characters, objects and cargo-hold below deck), a realistic-looking skycube example, a background sound loop, even ship flags with cloth colliders. The cloth colliders would work, but the ship object's flags have all been grouped together as one object so the cloth-physics are messed up (each flag should be a separate object with a separate cloth component). PS, All objects on the ship (including pirate and camer) are 'Child' objects of the ship so they sway and move with the ship, and it is currently self-guiding so you can set up a 1st or 3rd person controller and walk around on the ship while it sails. You could also set up a particle emitter for 'ocean spray' at the front of the boat as it sails forward, just make the emitter use the ship's bSpeed (from boatSail.cs) and you can have 'ocean spray' that automatically (according to speed) grows as you speed up, shrinks if you slow down, and disappears if you stop.

avatar image Honorsoft · May 08, 2018 at 02:42 AM 0
Share

I tried different methods of making a rippling water mesh to go with this, but I got the best results adapting a 'procedural landscape' project to make a water mesh. If anyone's interested, search (maybe github or Asset Store) for "Procedural Landscape Unity Project".

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to start developing for iOS? 0 Answers

What Emulator For Android Is The Best For Unity 3D? 0 Answers

What should I do? 2 Answers

How should I approach (or go about) developing games? 1 Answer

Help with Enemy IA walk-Attack-Retreat Jumping-wait-repeat 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges