Search Unity

OnMouseDrag

Discussion in 'Scripting' started by davejones1, Mar 19, 2018.

  1. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am trying to use the OnMouseDrag function to slow down the speed at which a UI slider can be moved. I am using a UI slider to scrub through an animation. I want to limit the speed at which the slider can be moved by the user so that the animation runs smoothly. So far I haven't managed to solve the issue. The script I am using has been attached.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class sliderscript : MonoBehaviour {
    7.  
    8.     public void OnMouseDrag () {
    9.  
    10.         Vector3 MousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    11.         MousePos.z = 0.2f;
    12.         transform.position = MousePos;
    13.  
    14.     }
    15.  
    16. }
    17.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you think the OnValueChanged of the slider could maybe help you and limit the next move to a small amount of time in the future?
     
  3. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    This could be a potential solution, however I wouldn't know how to implement this as I have very little coding experience.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Unity makes things fun and easy, but there's nearly no way to avoid programming. You can get a long way with even some knowledge. It's good to try to learn the basics.

    Not sure if this is exactly what you're looking for...
    Code (csharp):
    1. public class Test13 : Slider
    2. {
    3.     float nextTime = 0;
    4.     float currentValue = 0;
    5.     bool changed = false;
    6.  
    7.     public void SliderValueChanged(float val)
    8.     {
    9.         changed = true;
    10.     }
    11.     void LateUpdate()
    12.     {
    13.         if (!changed) return;
    14.         if (Time.time < nextTime)
    15.         {
    16.             value = currentValue;
    17.             changed = false;
    18.             return;
    19.         }
    20.         if (value < currentValue) currentValue--;
    21.         else if (value > currentValue) currentValue++;
    22.         value = currentValue;
    23.         nextTime = Time.time + .4f;
    24.         changed = false;
    25.     }
    26. }
     
    davejones1 likes this.
  5. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am keen to learn more about programming, its just that I have a deadline so learning what I need is difficult.
     
  6. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am going to add this to the slider Gameobject. I will let you know the outcome.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It baffles me when people say they have a deadline, and thus "skip learning". How can that even equate? :)
    (this is not the first time I've heard that..) heh.

    Hope it works for ya. I mean, when I tested it, it worked as far as restricting the slider's movement. Beyond that scope, I can't say ;)
     
  8. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Not skip learning as such, but having to look to find solutions in short spaces of time. Thus, making it difficult to learn what I want. I want to learn how to create code, but in some cases my knowledge of coding wont allow me to solve the problem in time for the deadline.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I just don't understand what it means in general. Is this for school?

    How can there be a deadline that is too constrained to give you the time to learn what you need?
    Anyways.. let me know how the code works lol :)
     
  10. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I have added your script and it doesn't seem to work.
     
  11. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Deadline meaning I need to deliver something by a set time, however I don't have the time to learn and implement what I need within the time frame I am given.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, you have to delete the original slider script, eh? Did you do that? I mean that's how I wrote it.. could probably change that.. but just delete the original and remember to put the 3 parts linked back in (that the original has.. references to parts of the slider).
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I knew what you meant, and not to keep going off topic, but how could you be given a deadline that you can't reach by learning or doing.. just seems strange to me. :)
     
  14. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Just the way things have turned out. I am a student and my lecturers have poorly organised the course.
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Did you see my post (two up?).
     
  16. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I deleted the slider component and added your script. I than added the references. The printscreen below shows what I have done.
     

    Attached Files:

  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I see. Okay, in my example I was using whole numbers 1 - 10 (to be specific) for testing.

    Try that.. and see if you even like it before doing more changes. Of course for 0 - 1.0f you'd have to modify the values a bit. ;)
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh, and you should add an OnValueChanged event for the method in the class there.'SliderValueChanged'. I can't see if that's so in your screenshot.
     
  19. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I have added the SliderValueChanged method to the OnValueChanged event. The slider moves much slower now, however it doesn't seem to move smoothly. I have tried playing around with the values but the smoothness of the slider doesn't seem to change.
     
  20. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Must be a way of smoothing it out using smoothdeltatime or something like that.
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, okay.. well that's a different story ;)
     
  22. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    In
    In what way? I have tried using smoothdeltatime but had no luck.
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, in the sense that I didn't consider that from your original question :)

    I was only thinking you wanted its value to change more slowly..
     
  24. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    I am trying to use the OnMouseDrag function to slow down the speed at which a UI slider can be moved. I am using a UI slider to scrub through an animation. I want to limit the speed at which the slider can be moved by the user so that the animation runs smoothly. So far I haven't managed to solve the issue. The script I am using has been attached."

    My original question is shown above, stating that I would like the animation to run smoothly. Thus the slider would have to be moved smoothly.
     
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, ya, I suppose I don't really understand.
     
  26. davejones1

    davejones1

    Joined:
    Jan 19, 2018
    Posts:
    183
    Is there a way to make the slider slower and smoother?