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 /
avatar image
0
Question by verschurengiovanni · Aug 30, 2019 at 10:08 AM · unity 5script.

Smoothly increase rotation speed when pressing a key

Hi,

I'm fairly new to unity and C#, so forgive me for this noob question.

I'm making a funfair ride, and I want to increase the rotation of an object when someone presses a key, for example "W" and "S" The code that I created now is simple, it goes to the speed I set, but it doesn't smoothly increase overtime while pressing the key.

Can someone help me with it?

Thanks!!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Kruis : MonoBehaviour
 {
   
 
     void Start()
     {
 
     }
 
     void Update()
     {
         if (Input.GetKey("w")) transform.RotateAround(transform.position, transform.up, Time.deltaTime * 210f);
         if (Input.GetKey("s")) transform.RotateAround(transform.position, transform.up, Time.deltaTime * 0f);
 
     }
 
 }
 
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

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Hellium · Aug 30, 2019 at 10:08 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Kruis : MonoBehaviour
 {
     public float RotationSpeed = 1;
     public float RotationAcceleration = 1;
     public float MaxRotationSpeed = 50;
     public KeyCode AccelerationKey = KeyCode.W;
     public KeyCode DecelerationKey = KeyCode.S;
     public Vector3 RotationAxis = Vector3.up;
     public bool RotateAroundWorldAxis;
     public bool RotateAntiClockWise;
     private float currentRotationSpeed;
 
     void Start()
     {
         currentRotationSpeed = RotationSpeed;
     }
 
     void Update()
     {
         if (Input.GetKey(AccelerationKey))
         {
             currentRotationSpeed += Time.deltaTime * RotationAcceleration;
         }
         if (Input.GetKey(DecelerationKey))
         {
             currentRotationSpeed -= Time.deltaTime * RotationAcceleration;
         }
 
         currentRotationSpeed = Mathf.Clamp(currentRotationSpeed, 0, MaxRotationSpeed);
 
         float angle = currentRotationSpeed;
         if (RotateAntiClockWise)
             angle = -angle;
 
         Vector3 rotationAxis = RotationAxis;
         if (!RotateAroundWorldAxis)
             rotationAxis = transform.rotation * RotationAxis;
         transform.RotateAround(transform.position, rotationAxis, angle);
     }
 
 #if UNITY_EDITOR
     private void OnDrawGizmosSelected()
     {
         Quaternion rotation       = RotateAroundWorldAxis ? Quaternion.identity : transform.rotation;
         Vector3 rotationAxis      = rotation * RotationAxis;
         float angle               = RotateAntiClockWise ? 1/2f : 1;
         float radius              = 0.5f;
         Color gizmosColor         = Color.magenta;
         Color handlesColor        = UnityEditor.Handles.color;
         Vector3 arrowCap          = transform.position + rotationAxis + transform.rotation * new Vector3(Mathf.Cos(angle * Mathf.PI), 0, Mathf.Sin(angle * Mathf.PI)) * radius;
         Vector3 leftArrowEnd      = arrowCap + Quaternion.Euler(0, RotateAntiClockWise ? 0 : 90, 0) * rotation * new Vector3(0.1f, 0, 0.1f) ;
         Vector3 rightArrowEnd     = arrowCap + Quaternion.Euler(0, RotateAntiClockWise ? 0 : 90, 0) * rotation * new Vector3(0.1f, 0, -0.1f);
         UnityEditor.Handles.color = gizmosColor;
         Debug.DrawLine(transform.position - rotationAxis, transform.position + rotationAxis, gizmosColor);
         UnityEditor.Handles.DrawWireArc(transform.position + rotationAxis, rotationAxis, transform.forward, 270, radius);
         Debug.DrawLine(arrowCap, leftArrowEnd, gizmosColor);
         Debug.DrawLine(arrowCap, rightArrowEnd, gizmosColor);
         UnityEditor.Handles.color = handlesColor;
     }
 #endif
 }


ORIGINAL ANSWER

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Kruis : MonoBehaviour
 {
     public float RotationSpeed = 1;
     public float RotationAcceleration = 1;
     private float currentRotationSpeed;
 
     void Start()
     {
         currentRotationSpeed = RotationSpeed;
     }
 
     void Update()
     {
         if (Input.GetKeyDown("w") || Input.GetKeyDown("s"))
         {
             currentRotationSpeed = RotationSpeed;
         }
         if (Input.GetKey("w"))
         {
             transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
             currentRotationSpeed += Time.deltaTime * RotationAcceleration;
         }
         if (Input.GetKey("s"))
         {
             transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
             currentRotationSpeed += Time.deltaTime * RotationAcceleration;
         }
     }
 }

Comment
Add comment · Show 8 · 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 verschurengiovanni · Aug 30, 2019 at 10:18 AM 0
Share

Thanks! That totally works! Amazing. But it doesn't hold the speed.

I want "W" to go faster and "S" to slow down.

is that possible?

avatar image Hellium verschurengiovanni · Aug 30, 2019 at 10:56 AM 2
Share

Do you want the object to rotate automatically, and rotate it faster and faster while W is pressed, and slow it down when S is pressed?

If so, the script must be different.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class $$anonymous$$ruis : $$anonymous$$onoBehaviour
 {
     public float RotationSpeed = 1;
     public float RotationAcceleration = 1;
     private float currentRotationSpeed;
 
     void Start()
     {
         currentRotationSpeed = RotationSpeed;
     }
 
     void Update()
     {
         if (Input.Get$$anonymous$$ey("w"))
         {
             currentRotationSpeed += Time.deltaTime * RotationAcceleration;
         }
         if (Input.Get$$anonymous$$ey("s"))
         {
             currentRotationSpeed = $$anonymous$$athf.$$anonymous$$ax( currentRotationSpeed - Time.deltaTime * RotationAcceleration, 0 );
         }
         
         transform.RotateAround(transform.position, transform.up, currentRotationSpeed);
     }    
 }
avatar image verschurengiovanni Hellium · Aug 30, 2019 at 10:59 AM 0
Share

No, I want "W" to start the rotation and "S" to slow down and eventually stop the rotation.

Thanks for the help thus far! :)

Show more comments
avatar image Hellium · Aug 30, 2019 at 12:33 PM 1
Share

I've added a new code to my answer which is pretty complete I believe. You can customize several things directly in the inspector.

avatar image verschurengiovanni Hellium · Sep 06, 2019 at 07:13 PM 0
Share

Now I need one that rotates on its X Axis, but I'm not sure on how to that. Could you perhaps help me again?

avatar image Hellium verschurengiovanni · Sep 06, 2019 at 07:23 PM 0
Share

Have you tried to attach the component twice, change the axis and the keys?

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

216 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 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 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

How to use Animator Override Controler in script? 0 Answers

Random generation 1 Answer

How to assign script to inspector variable 2 Answers

Unity 5 C#, having a script show up in a path in the Editor 1 Answer

Encrypting / Verifying script files 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