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 Bejasc · Dec 19, 2012 at 07:53 AM · c#inputspacerigid body

RigidBody.AddRelativeTorque // Input.GetButtonDown issues

Hello there, I've recently begun delving into Unity and C#, and have attempted to make a simple 3D space game, where you fly around. Currently, everything works as intended, and I am using rigidbody.AddRelativeTorque to change the pitch and roll of the object. I have a few issues.

Firstly, the controls seem rather clunky. I've experimented with Input.GetKey, .GetKeyDown, .GetButton .GetButtonDown, etc, in an attempt to make it so that the actions only happen whilst the key is actually being physically held down, and NOT happening once it is released. Currently if the key is tapped, it will continue to move until the opposite key is tapped, upon which it will slow down (but not stop)

The second issue that I am having is that this is happening too quickly. The changes occur much quicker than desiered, and I have played around with the "rollspeed" value in an attempt to change this, with no luck.

I've not been working with C# and Unity for very long, so please forgive my inexperience. My code is as follows.

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
 
     // Use this for initialization
         public float rollspeed = 0.01f;
          void Update()
         {
             ///////////////////////////ROLL LEFT///////////////////////
             if (Input.GetButtonDown("Roll") && Input.GetAxis("Roll") < 0)
             {
                 Debug.Log("Roll Left"); //Display it is picking up that you've pressed the button
                 rigidbody.AddRelativeTorque(0,0, rollspeed); //MOVES CORRECTLY but too fast
             }
             ///////////////////////////ROLL RIGHT///////////////////////
             if (Input.GetButtonDown("Roll") && Input.GetAxis("Roll") > 0)
             {
                 Debug.Log("Roll Right"); 
                 rigidbody.AddRelativeTorque(0, 0, -rollspeed);
             }   
             ///////////////////////////PITCH UP///////////////////////
             if (Input.GetButton("Pitch") && Input.GetAxis("Pitch") > 0 )
             {
                 Debug.Log("Pitch Up"); 
                 rigidbody.AddRelativeTorque(rollspeed, 0, 0);
             }
             ///////////////////////////PITCH DOWN///////////////////////
             if (Input.GetButton("Pitch") && Input.GetAxis("Pitch") < 0 )
             {
                 Debug.Log("Pitch Down"); 
                 rigidbody.AddRelativeTorque(-rollspeed, 0, 0);
             }      
         }
         }


I would like to know what I need to change in order to;

-Limit the speed these changes take placed (preferably based upon a variable)

-Make these changes ONLY when the button is held

If someone could assist me in working around my problem, I would greatly appreciate it. (This is the first time I've used Unity Answers. Apologies for any incorrect formatting)

Regards,

Ben

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bejasc · Dec 19, 2012 at 10:37 PM

After fiddling around I have stumbled upon a solution. The new code is now

If someone could confirm that this new way is the most efficient or reliable way to do what I have asked it to do, or if there is another, better way, please let me know

 ///////////////////////////ROLL LEFT///////////////////////
 if (Input.GetKey(KeyCode.A))
 {
     Debug.Log("A key is Down");
     rigidbody.transform.Rotate(0, 0, rollspeed*2);
 }

 ///////////////////////////ROLL RIGHT///////////////////////
 if (Input.GetKey(KeyCode.D))
 {
     Debug.Log("D key is Down");
     rigidbody.transform.Rotate(0, 0, -rollspeed*2);
 }


 ///////////////////////////PITCH UP///////////////////////
         if (Input.GetKey(KeyCode.S))
 {
     Debug.Log("Pitch Up");
     rigidbody.transform.Rotate(-rollspeed, 0, 0);
 }

 ///////////////////////////PITCH DOWN///////////////////////
 if (Input.GetKey(KeyCode.W))
 {
     Debug.Log("Pitch Down");
     rigidbody.transform.Rotate(rollspeed, 0, 0);
 }
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
avatar image
0

Answer by Banksy · Aug 15, 2013 at 09:46 AM

I believe void Update() is not efficient when using physics..
void FixedUpdate() is much better.

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

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Swipe and Hold to move character . 1 Answer

Missed key input 1 Answer

Hover Over Input Field Before Inputting? 2 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