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
1
Question by justaleaf · Apr 12, 2014 at 11:59 PM · c#rotationtransformvector3input.getaxis

Rotating object on one axis using Input.GetAxis

I'm very new to C# and I've been reading the lessons and anything I could find here on Unity Answers. I've been failing to understand this code... not sure if I'm close or very far off.

 using UnityEngine;
 using System.Collections;
 
 public class RotateTriggers : MonoBehaviour 
 {   
     public float speed = 5.0f;
     
     void Update () {
         
         Vector3 v3 =  Vector3(0.0, Input.GetAxis("Horizontal"), 0.0);
         transform.Rotate(v3 * speed * Time.deltaTime);
 
     }
 }

All I'm looking to accomplish is to use an axis control (in this case the Xbox 360 Triggers) to rotate an object smoothly on one axis (I understand that may not be strictly what is happening).

Any help appreciated (if I'm going about this completely the wrong way, please feel free to change my approach).

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
5
Best Answer

Answer by robertbu · Apr 13, 2014 at 01:25 AM

When posting a question, please explain what is going on, and what is your desired behavior. For example I can see the code above would not compile, so including the compiler error messages (copied directly from the console) is really helpful in getting to an answer. As for your code, you can simplify it to:

 using UnityEngine;
 using System.Collections;

 public class RotateTriggers : MonoBehaviour {   
     public float speed = 5.0f;
     
     void Update () {
         transform.Rotate(0.0f, -Input.GetAxis ("Horizontal") * speed, 0.0f);
     }
 }

Note in this case, you don't need to use Time.deltaTime since that is factored to some degree by the value return by Input.GetAxis().

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 justaleaf · Apr 13, 2014 at 02:16 AM 0
Share

Thank you for the note on Time.deltTime. I really didn't consider the relevence of the axis position... very cool.

While waiting for an answer on this I was getting even more lost trying to get this working with Quaternion.Euler which, while interesting, turned out to be the wrong choice here. I can tweak the rest from the Input settings as far as sensitivity and speeds go.

(and duly noted about the compile error. I will be sure to be more detailed and provide such details in the future)

Appreciated!

avatar image robertbu · Apr 13, 2014 at 04:29 AM 0
Share

Transform.Rotate() is a relative rotation that is in local space. Transform.eulerAngles is a absolute rotation in world space. You could do something similar to the code above using eulerAngles like this:

 using UnityEngine;
 using System.Collections;
 
 public class RotateTriggers : $$anonymous$$onoBehaviour 
 {   
     public float speed = 5.0f;
     private float rot = 0.0f;
     
     void Update () {
         rot -= Input.GetAxis ("Horizontal") * speed;
         transform.eulerAngles = new Vector3(0.0f, rot, 0.0f);
     }
 }

I may have dismissed the use of 'deltaTime' too quickly. It depends on what is tied to the "Horizontal" axis. If it is a button, then you would want to use deltaTime (and you would have to substantially increase 'speed').

avatar image kfidanci · Oct 01, 2019 at 05:57 AM 0
Share

after i turn it, even tho im not pressing the button, it still continue turning. how can i fix that?

avatar image
0

Answer by QQQ_QQQ · Nov 05, 2019 at 10:17 PM

Thanks man!

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

23 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 avatar image avatar image avatar image

Related Questions

How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers

Instantiate GameObject towards player 0 Answers

Transform a Vector by a Quaternion 1 Answer

Multiple Fire on different launcher GameObjects 1 Answer

Rotations and waypoints 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