Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by PaxStyle · Apr 02, 2014 at 06:54 PM · rotationinput

Arm Rotation.

Hi guys, i did this script to make a right rotation (if press A, increasing right speed) and left rotation (if press D increasing left speed), but the script works bad. The rotation is jerky.. If i remove the keys input, the script works well, so the problem is there.. hope yu can help me, any help is good. Thank you.

 using UnityEngine;
 using System.Collections;
 
 public class Gondola : MonoBehaviour {
 
     public GameObject gondelarm;
     float gondelarm_sin;
 
 
 void Awake()
 {
     gondelarm_sin = 0.0f;
 }
 
 void Start()
 {
 
 }
 
 void Update()
 {
     //-- right arm rotation
     if(Input.GetKeyDown(KeyCode.A)){
 
     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);
     
 }
     //-- left arm rotation
     if(Input.GetKeyDown(KeyCode.D)){
             
     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);
             
 }
 
     //-- brake arm rotation
     if(Input.GetKeyDown(KeyCode.S)){
             
     float arm_rot = Mathf.Sin(gondelarm_sin) * 80.0f;
     gondelarm_sin += (gondelarm_sin + Time.deltaTime * 0.9f) % 360.0f;
     gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
             
 }
 
 
 
 }
 
 }
Comment
Add comment · Show 5
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 · Apr 02, 2014 at 08:19 PM 0
Share

A couple of things jump out a me. You are mixing degrees and radians. $$anonymous$$athf.Sin() returns radians. Second, can't you just use:

  transform.Rotate(0f, 0f, speed * Time.deltaTime)

...and have your A and D keys increment/decrement 'speed'?

avatar image robertbu · Apr 03, 2014 at 03:28 PM 0
Share

Your original question only mentions rotation, but with the use of $$anonymous$$athf.Sin() what you are really seem to be after is something that swings back and forth. Correct?

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

exactly... ;)

avatar image robertbu · Apr 03, 2014 at 03:57 PM 0
Share

Okay, so the object is swinging. What is this left vs right speed thing? You want it to swing faster in one direction than the other? And what is this 'brake arm rotation'? Does that slow both sides down?

avatar image PaxStyle · Apr 03, 2014 at 04:26 PM 0
Share

So.. i try to explain, If i press A, add force to right, If I press D, add force to left. If I press S, decrease the current speed, until stop it.

So, this script works well, but missing the input, and the increasing of the speed. (That i tried to add in the code that i've posted before), but the movement that i would like are so.

 using UnityEngine;
 using System.Collections;
 
 public class Gondola : $$anonymous$$onoBehaviour {
 
 
     public GameObject gondelarm;
     float gondelarm_sin;
 
 
 void Awake()
 {
     gondelarm_sin = 0.0f;
 }
 
 void Start()
 {
 
 }
 
 void Update()
 {
     //-- arm rotation
     float arm_rot = $$anonymous$$athf.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);
     
 }
 
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 03, 2014 at 07:13 PM

In Unity, typically a problem like this one would be handled by the physics engine. Simulating physics like this without some substantial research will be hit and miss. Here is a bit of code in the direction of what I think you are trying to do. It's not perfect, and you may have to do substantial additional work to get what you want, but it should provide you a starting point.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
 
     public GameObject gondelarm;
     private float gondelarm_sin = 0.0f;
     public float gondelSpeed = 1.0f;
     public float increaseFactor = 0.5f;
     public float maxAngle = 80f;
     public float breakingFactor = 0.9f;
 
     private float prevRot = 0.0f;
     private bool right = false;
 
     void Update()
     {
         //-- right arm rotation
         if(Input.GetKeyDown(KeyCode.A))
         {
             if (right)
                 gondelSpeed -= increaseFactor;
             else
                 gondelSpeed += increaseFactor;
         }
         //-- left arm rotation
         if(Input.GetKeyDown(KeyCode.D))
         {
             if (right)
                 gondelSpeed += increaseFactor;    
             else
                 gondelSpeed -= increaseFactor;
         }
         
         //-- brake arm rotation
         if(Input.GetKeyDown(KeyCode.S)){
             gondelSpeed *= breakingFactor;
         }
 
         gondelarm_sin += gondelSpeed * Time.deltaTime;
         float arm_rot = Mathf.Sin(gondelarm_sin) * maxAngle;
         right = prevRot < arm_rot;
         prevRot = arm_rot;
         gondelarm.transform.localEulerAngles = new Vector3(0.0f, 0.0f, arm_rot);
     }
 }
Comment
Add comment · 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

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

20 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

Related Questions

Object Rotation doesnt work 0 Answers

Need help on the 3ds max style camera control 0 Answers

[Problem] "Security Camera" rotation bounds - stop rotation if player goes past a specific point 1 Answer

Moving an object in a circle towards joystick 0 Answers

Rotation Constraint doesn't work properly 1 Answer


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