• 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 · Apr 28, 2020 at 09:24 PM · scripting problemscripting beginner

Gently increase speed

I have made a simple script wich rotates an object on a key press. But I want to gently increase the speed to an X amount and when a user lets go of the key it gently decreases to 0.

I'm not sure on how to that, so any help is much appreciated

The script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Swing : MonoBehaviour
 {
 
 
     void Start()
     {
 
     }
 
     void Update()
     {
         if (Input.GetKey("w")) transform.RotateAround(transform.position, transform.forward, Time.deltaTime * 10f);
         if (Input.GetKey("s")) transform.RotateAround(transform.position, transform.forward, Time.deltaTime * -10f);
 
     }
 
 }


The W key rotates the object a certain way and the S key rotates the object a certain way.

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

3 Replies

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

Answer by Hellium · Apr 28, 2020 at 10:05 PM

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Swing : MonoBehaviour
  {     
      [SerializeField] private float minRotationSpeed = -10;
      [SerializeField] private float maxRotationSpeed = 10;
      [SerializeField, Min(0.001f)] private float rotationAcceleration = 1;
      [SerializeField, Min(0)] private float rotationDeceleration = 1;
      private float rotationSpeed;
  
      void Update()
      {
          if (Input.GetKey("w"))
             rotationSpeed = Mathf.MoveTowards(rotationSpeed, maxRotationSpeed, rotationAcceleration * Time.deltaTime);
         
          if (Input.GetKey("s"))
             rotationSpeed = Mathf.MoveTowards(rotationSpeed, minRotationSpeed, rotationAcceleration * Time.deltaTime);
 
          if (!Input.GetKey("w") && !Input.GetKey("s"))
              rotationSpeed = Mathf.MoveTowards(rotationSpeed, 0, rotationDeceleration * Time.deltaTime);
          
          transform.RotateAround(transform.position, transform.forward, Time.deltaTime * rotationSpeed);
      }
  }
Comment
Add comment · Show 1 · 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 · Apr 29, 2020 at 11:04 AM 0
Share

Thank you so much. This totally fits my needs!

avatar image
0

Answer by Cassos · Apr 28, 2020 at 09:38 PM

This Should work out:

     public float rot_speed = 100;
     public float rot_speedUp = 3;
     Quaternion targetRotation;
     void Start()
     {
         targetRotation = transform.rotation;
     }
 
     void Update()
     {
         float delta = Time.deltaTime;
 
         if (Input.GetKey("w"))
             targetRotation.eulerAngles += new Vector3(0, 0, rot_speed * delta); // Choose your axis, I chose z
         else if (Input.GetKey("s")) // Prevents bugging in any way when both keys would be pressed
             targetRotation.eulerAngles -= new Vector3(0, 0, rot_speed * delta); // Choose your axis, I chose z
 
         transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, delta * rot_speedUp); // Applying the rotation
     }

Greetings,

Max

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 toddisarockstar · Apr 28, 2020 at 09:43 PM

you can use a secondary number that keeps track of the time that the keys where pressed down and then multiply your speed by the new number

     public float downtime;
         
 
         
         void Update()
         {
         bool down = false;
         if (Input.GetKey("w")){ down = true;transform.RotateAround(transform.position, transform.forward, downtime*Time.deltaTime * 10f);}
         if (Input.GetKey("s")){ down = true; transform.RotateAround(transform.position, transform.forward, downtime*Time.deltaTime * -10f);}
         if (down) {if(downtime<1){downtime+=Time.deltaTime;}}else{downtime = 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

GetComponent won't work 1 Answer

creating and using list of prefabs 2 Answers

Can I change animation key frame property in script? 1 Answer

How to Write a Drag and Drop script for Unity 5.3.3? 4 Answers

How can i upgrade the fire rate and damage in my upgrade menu 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