• 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 /
avatar image
1
Question by PaxStyle · Jul 12, 2013 at 07:40 PM · rotationforce

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.. ;) 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.. :(

 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.. :( I hope you can help me, and sorry for my bad english!

Paolo

Comment
Add comment · Show 3
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 robertbu · Jul 12, 2013 at 08:09 PM 0
Share

Just to be clear for anyone who wants to answer this question, when the key is released, does it swing just once back to the original position, or do you mean by "with gravity" the boat swings back and forth like a pendulum until it comes to rest. And what should happen if the key for left rotation is pressed until the boat is swung far left and then the key for right rotation is pressed?

avatar image PaxStyle · Jul 12, 2013 at 09:10 PM 0
Share

For robertbu:

If release the keys, the rotation oscillates for a few seconds, then stop, yes like a pendulum. (whit the gravity force returns to the original position (photo that is in the middle http://imageshack.us/a/img407/963/pgv.png )). If press the right arrow, the ship rotate in right direction, and if at the same time press the left arrow, the right rotation decrements, and give strength to the left rotation..and vice versa. For give force at the rotation ship, must press left arrow and right arrow, right and left, right and left........ :)

avatar image Slobdell · Jul 13, 2013 at 01:10 AM 0
Share

Are you using a joint? It seems to me if you used a joint you could just apply a force on either the left or right front of the ship and the gravity and slowing down and stuff would just take care of itself

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jul 13, 2013 at 03:23 AM

@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:

alt text

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.


hinge.jpg (22.6 kB)
Comment
Add comment · Show 3 · 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 PaxStyle · Jul 13, 2013 at 12:28 PM 0
Share

@robertbu, thank you so much for your complete reply, everything is clear. But when I add the J.Script that you made, appears this error:

http://oi44.tinypic.com/lk6qr.jpg

The Script is this:

 #pragma strict
 
 var amount = 5.0;
 
 function Start () {
 
 }
 
   function Update () {
  
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
            rigidbody.AddForce(transform.right * amount);
         }
         else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
            rigidbody.AddForce(-transform.right * amount);
         }
     }



so I could not see if the end result is correct.. :)

avatar image robertbu PaxStyle · Jul 13, 2013 at 03:06 PM 0
Share

I get this error every once in a while. Try copying the script to the clipboard, deleting the script from the project, shutting down Unity and $$anonymous$$ono, restarting Unity, and re-adding the script. Sometimes when restarting Unity, I see an error in another script in the Console window that I did not see before.

avatar image PaxStyle · Apr 08, 2014 at 06:03 PM 0
Share

Hi Robert, can i ask you a question? How can i restrict the object rotation to certain amount in the script that you did? i have to create a var "max amount"?

Thank you

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Velocity bug with hinge joints forces in C# 0 Answers

Rotation with Force 0 Answers

How to apply a force forward depending of the camera 2 Answers

Object pulling to a perpendicular angle when on a slope 0 Answers

Unwanted rotation 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