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);
     }
 }

}

1 Answer

1

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:

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".