Rotation Force

Hi to all!
I would make if I press left arrow the boat rotate at left, and if i press right arrow, the boat rotation is at right… and if I release this two taste, the boat with gravity force, return at the original position… (slowly)

Here’s a picture of the movement that I try to do… :wink:
http://imageshack.us/a/img407/963/pgv.png

I did this script, but is no good…
I don’t know how add gravity force, and the rotation is jerky… and if I press the oppost key, instead of slowing down the rotation, immediately goes on the opposite side without rotation… :frowning:

public class RideSchiffschaukel : MonoBehaviour
{
    GameObject gondelarm;
    float gondelarm_sin;

void Awake()
{
    gondelarm = transform.Find("plattform/staender/gondel_arm").gameObject;
    gondelarm_sin = 0.0f;
}

void Start()
{

}

//Update viene chiamata ad ogni istante
void LateUpdate()
{
    //-- Left arm rotation
    if(Input.GetKeyDown(KeyCode.LeftArrow))
    {
    float arm_rot = Mathf.Sin(gondelarm_sin) * 80.0f;
    gondelarm_sin = (gondelarm_sin + Time.deltaTime * 1.0f) % 360.0f;

    gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
    }

    //--Right arm rotation
     if(Input.GetKeyDown(KeyCode.RightArrow))
    {
    float arm_rot = - Mathf.Sin(gondelarm_sin) * 80.0f;
    gondelarm_sin = (gondelarm_sin + Time.deltaTime * 1.0f) % 360.0f;

    gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
    }    
}
} 

I’m trying to do this 1 month… but I can’t get… :frowning:
I hope you can help me, and sorry for my bad english!

Paolo

@Slobdell has the right idea (assuming you are open to using the physics engine). Use a hinge joint. If you haven’t used hinge joints before, here is a video tutorial to get you started:

http://www.unity3dstudent.com/2010/07/beginner-b11-basic-joints/

Here is a possible setup for your game:

  • Place an empty game object at the apex of your four support columns. Add a Rigidbody and set the IsKinematic flag to ‘true.’
  • Add a Rigidbody component to your Boat.
  • Add a Hinge component to the Boat. Make the axis (0,0,1) assuming the camera is looking towards positive ‘Z’. Adjust the anchor ‘Y’ until the hinge is at or near your empty game object from step 1.
  • Drag and drop the empty game object from the first step onto the Connected Body variable in the hinge joint.

After all the setup, the script is trivial:

   var amount = 5.0;

    function Update () {
    
    	if (Input.GetKey(KeyCode.RightArrow)) {
    		rigidbody.AddForce(transform.right * amount);
    	}
    	else if (Input.GetKey(KeyCode.LeftArrow)) {
    		rigidbody.AddForce(-transform.right * amount);
    	}
    }

I ran a quick test. Here is how I setup the hinge:

13095-hinge.jpg

Note the force is being applied at the pivot. You may want to adjust the pivot in your modeling program, or you want to use AddForceAtPoint(). Also you may want to play with the mass and scale of the ship and the force applied to get the motion you want.