Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 01, 2017 at 04:20 PM by Anonymou5 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Anonymou5 · Sep 23, 2016 at 04:28 PM · objectrotatez-axiskey pressedslowly

How to rotate object (z-axis only) slowly? C#

I'm trying to make simple moped and i want it to rotate on z-axis when key A or D is pressed.

When i press D to turn right -> moped rotates 30 degrees right (slowly) When i press A to turn left -> moped rotates 30 degrees left (slowly)

Any ideas how i can do that?

I'm using C#

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

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by King_Kurl · Sep 23, 2016 at 06:43 PM

Here you go: Just attach this script to whatever gameobject you want, assign the gameobject you want to rotate under target and set the rotation speed to whatever you want and you should be good to go. I haven't tested this but I'm pretty sure it should work.

 using UnityEngine;
 using System.Collections;
 
 public class RotateOnZAxis : MonoBehaviour {
 
     //Target rotation speed
     public float rotateSpeed = 0.5f;
 
     //Max angle rotation
     public float maxAngle = 30.0f;
 
     //Rotationtarget
     public Transform target;
 
     //Origin Angle
     public float originAngle = 0.0f;
 
     //Reset position?
     public bool resetPosition;
 
     //The Current Angle
     public float currentAngle;
 
     //Actual rotation speed
     private float rotSpeed;
 
     void Update(){
 
         //Calculate target rotation speed
         rotSpeed = rotateSpeed * Time.deltaTime;
 
         //Get the current angle
         currentAngle = target.localEulerAngles.z;
     
         //If current value is between 180 and 360 degrees, get it's negative equivalent
         //This just makes the math easier
         currentAngle = (currentAngle > 180) ? currentAngle - 360 : currentAngle;
 
         //If the current angle is greater than the max angle, set it equal to the max angle
         if (currentAngle > maxAngle) {
             currentAngle = maxAngle;
         }
         //Or If the current angle is smaller than the negative max angle equivalent, 
         //set it equal to the negative max angle equivalen
         else if(currentAngle < -maxAngle){
             currentAngle = -maxAngle;
         }
 
 
         //If press A, rotate the angle to the left, at the specified speed
         if (Input.GetKey (KeyCode.A)){
             if (currentAngle < maxAngle) {
                 target.Rotate (0, 0, rotSpeed);
             }
         }
 
         //If press D, rotate the angle to the right, at the specified rotation speed
         if (Input.GetKey (KeyCode.D)) {
 
         if (currentAngle > -maxAngle) {
                 target.Rotate (0, 0, -rotSpeed);
             
             }
         }
 
         //If either A or D is released, then set reset position to true
         if (Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.D)) {
             resetPosition = true;
         }
 
         //If reset position is set to true
         if (resetPosition == true) {
         
             //And as long as A or D are not pressed 
             if ((!Input.GetKey (KeyCode.A) && !Input.GetKey (KeyCode.D))) {
 
                 //If the current angle is greater than the origin angle then decrease it's value
                 if (currentAngle > originAngle) {
                     target.Rotate (0, 0, -rotSpeed);
                 } 
                 //Or if the current angle is less than origin angle then increase it's value
                 else if (currentAngle < originAngle) {
                     target.Rotate (0, 0, rotSpeed);
                 }
 
                 //If the current angle is between 0.1 and -0.1 degrees, then set reset to false
                 if (currentAngle < 0.1f && currentAngle > -0.1f) {
                     currentAngle = 0;
                     resetPosition = false;
                 }
             }
         }
     }
 }
 
Comment
Add comment · Show 6 · 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 Anonymou5 · Sep 24, 2016 at 10:03 AM 0
Share

When i press D or A it rotates my moped only 30 degrees just like i wanted. But when i drive with my moped and turn it to left or right z-axis goes over 30 degrees.

How i can limit z-axis rotation?

Edit: I think i find solution but i still need help with it.

How to limit only one axis rotation? c#

avatar image Anonymou5 · Sep 24, 2016 at 01:37 PM 0
Share

Any ideas how i can get z-axis rotation get back to 0 when A or D key isn't pressed?

avatar image King_Kurl · Sep 24, 2016 at 05:42 PM 0
Share

$$anonymous$$y bad, I just saw your comment. There is a multitude of things that could be causing the issue, can you provide more details as to what your set up looks like, and maybe the code you're using to move the moped?

Sure I can help you with that, I'm in the middle of something at the moment so I'll work on it in a bit, give me an hour or two.

One more thing to keep in $$anonymous$$d, this isn't really a place to just ask for code, it's a place to ask for help with your code, and that is if you couldn't find a solution online. But I'm willing to help because you're a new member.

So, Just keep that in $$anonymous$$d for next time.

avatar image King_Kurl · Sep 24, 2016 at 07:36 PM 0
Share

Alright here you go, I updated the code above to include the added functionality. So now whenever A or D is released the moped should start moving back to the origin point. If you press A or D while it's resetting it's position it will stop the reset process and move in the desired direction, until A or D is released again.

avatar image Anonymou5 · Sep 24, 2016 at 08:57 PM 0
Share

Thank You! Now my moped rotation works.

avatar image King_Kurl Anonymou5 · Sep 24, 2016 at 10:07 PM 0
Share

Glad to help! Let me know if you encounter any other issues or need help with something.

Follow this Question

Answers Answers and Comments

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

Related Questions

Terrain to object? 0 Answers

Rotating & fixing the position of an object to one spot 1 Answer

Rotate object arround other object, but always facing the camera 1 Answer

Rotating Gameobject not Colliding 1 Answer

If successful collision reverse rotation of object 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