Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 AjelG · Dec 11, 2016 at 10:44 PM · dragginginertiadampingdrawer

How to add inertia damping?

I have the bellow code witch open-closes a drawer by mouse dragging. How do i add inertia?

Here is the code.

 using UnityEngine;
 using System.Collections;
 
 public class draw : MonoBehaviour {
 
     float translate_z;
     float current_z;
     float sensX = 4.0f;
 
     public bool isDraging;
     public GameObject drawer;
 
     void Update () {
         if (Input.GetMouseButton (0)) {
             if (!isDraging) {
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
                 if (Physics.Raycast (ray, out hit, 5.0f)) {
                     drawer = hit.collider.gameObject;
                     current_z = hit.collider.transform.position.z;
                     translate_z = current_z;
                     Debug.Log (current_z);
                     isDraging = true;
                 }
             }
 
             if (isDraging) {
                 translate_z += Input.GetAxis ("Mouse Y") * sensX * Time.deltaTime;
                 translate_z = Mathf.Clamp (translate_z, -1.25f, 0.0f);
                 drawer.transform.localPosition = new Vector3 (0.0f, 0.0f, translate_z);
             }
         } else if (Input.GetMouseButtonUp(0)) {
             isDraging = false;
         }
     }
 }
     
 

 
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
0

Answer by Konomira · Dec 11, 2016 at 11:12 PM

This should work, it adds a speed modifier that is increased every frame that the drawer is dragged, and is decreased every frame that it isn't.

 using UnityEngine;
 using System.Collections;
  
 public class draw : MonoBehaviour {
  
 float translate_z;
 float current_z;
 float sensX = 4.0f;
 float currentDragSpeed = 0; // The speed the drawer is being dragged
 float maxDragSpeed = 1f; // Change this to the fastest speed you want to be able to drag the drawer at
 float accelerationRate = 0.01f;    // Changing this value will affect how quickly the drawer accelerates
 
 public bool isDraging;
 public GameObject drawer;
 
 void Update () {
     if (Input.GetMouseButton (0)) {
         if (!isDraging) {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
             if (Physics.Raycast (ray, out hit, 5.0f)) {
                 drawer = hit.collider.gameObject;
                 current_z = hit.collider.transform.position.z;
                 translate_z = current_z;
                 Debug.Log (current_z);
                 isDraging = true;
             }
         }
 
         if (isDraging) 
         {
             if(currentDragSpeed < maxDragSpeed)    //    While the drawer can accelerate further
             {                 
                 currentDragSpeed += accelerationRate;    //    Accelerate
             }
         }
         else
         {
             if(currentDragSpeed > 0)    //    While the drawer is still moving
             {
                 currentDragSpeed -= accelerationRate;    //    Decelerate
         }
     }
 
     translate_z = Input.GetAxis ("Mouse Y") * sensX* currentDragSpeed * Time.deltaTime;
     translate_z = Mathf.Clamp (translate_z, -1.25f, 0.0f);
     drawer.transform.localPosition += new Vector3 (0.0f, 0.0f, translate_z);
 
     } else if (Input.GetMouseButtonUp(0)) {
              isDraging = false;
         }
     }
 }
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 AjelG · Dec 12, 2016 at 10:14 AM 0
Share

Thanks for your answer but unfortunately is not working. I can only drag out the drawer and with no inertia. However you pointed me in a good direction.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to do inertia damping 1 Answer

Rigidbody inertia recalculation after detaching child colliders ? 2 Answers

Error: Actor::updateMassFromShape 0 Answers

I need code to drag object to specific distance along y axis only? 0 Answers

Swipe/Drag to move Camera along Path/Track 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