Search Unity

drag an object in only path

Discussion in 'Scripting' started by freedom667, Sep 29, 2019.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I want to drag an object with my mouse but only in line path. how can I do this? the line can be bezier. So it won't flat line only

    mouse drag object line.JPG
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'd probably treat it like a graph. Use the mouse X coordinate as input and calculate the corresponding Y coordinate based on the shape of the curve.
     
  3. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    is not possible to do this in line? because I tried you said before but I could not get positive result
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What i wrote should work. You will have something that defines your line.
    Use this something to figure out which (y) position on the line you are on for any given (x) mouse position, and combine them to get the (x,y) position of your red ball that you are looking for. That's what you want, right? Basically you want to ignore the y position of the mouse and make it such that the "ball" always sticks to the line while the user drags it.
     
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    absolutely yes. think just like waypoint but with Mouse Drag not automatic move
     
  6. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    anybody knows? is there a slider like this shape
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What stops you from doing what i suggested? Did you try? What did not work?
    You probably did not get further replies because people thought it was already answered.

    And is it really supposed to be a slider? What are you going to use it for? More information always helps.
     
  8. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    Your suggested did not work because I tried it before. User will trace a shape but if I make it according to mouse position, then go outside anywhere of the shape. Actually I want it to work like slider. Because, if it working like slider, red ball can't go outside of the line or shape
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You did not read what i wrote then, since i described how to prevent the ball from moving outside the shape. Twice.
    Take the mouse coordinates, use it's x as input for the function describing the shape of your "slider", get the return of that and treat it as y. Then use this combination of (x,y) to set the position of the red ball.
    Let's assume your slider is supposed to have a parabolic shape, described through f(x) = x²
    You take the x of your mouse position, plug it into the function to get the result for x² and now use (x, x²) as the new position of the red ball. Since the function describes where the line is for any given input, this should make it impossible for the ball to leave the line. I described this twice now.

    If you describe your slider shape with some mathematical function this can easily be done exactly like above. But it doesnt have to be a mathematical function. However you may define your graph shape, you only need some way to get the position on the graph for any given x input. This works always, unless the graph can have two results for any given x coordinate (as in, the graph looks like an upright S shape for example), in which case you simply need to find the closest position instead.
     
  10. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    hmm. that's too detailed. I'm not professional enough to do it. I wish it was an asset about it but not there
     
  11. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    338
    I agree with Yoreki, that would be a smart way to do it.
    But since you don't have the tech knowledge to implement it, an easy way to do it is to use the line renderer, where you can manage the points of your line and access each position index and get the values from it to move your object in.
    https://docs.unity3d.com/Manual/class-LineRenderer.html

    To make the object move smoothly between points you can use DOTween, which is a free asset I use that has many useful features.
     
  12. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    it will be tooo difficult for me. I wish anoybody make it :)
     
  13. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    This is a forum. A forum is a place where you can exchange knowledge and get help on certan topics. It's not a place for you to outsource your work so others do it for you. People will explain to you how things are done, and if reasonable or necessary they will offer some code examples, but in the end you need to show that you actually plan on putting in some effort to get the result you want to have. Having a lot of problems is fine, but if you give up on doing it yourself because it's "too difficult for you", then also give up on the idea of creating what you want.
    This forum is not some cheap workforce for you to exploit when you dont feel like learning how to do it yourself..
     
  14. That's perfectly fine. On Unity Connect you can hire someone (either for a price or for some barter agreement) to do it for you.
     
  15. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    why did you get angry? I just said that if it was an asset, I would buy it if it was an asset. I didn't expect anyone to write code
     
  16. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    That's exactly what you wrote tho. I didnt want to be rude, but there are some people from time to time who come to the forum and expect others to do their work for them. And that gets frustrating really fast. Anyways, as Ninja said there are websites where you can hire somebody to do it for you, if you are willing to pay.

    But you could still try to do it yourself. It's really not that complicated, but i know how that sounds if it's a topic you dont feel comfortable with. How exactly do you get that line? You need to define it somehow. Your image looks like a triangle wave. Do you already have something in Unity to draw this line? If so, how do you do it? That's where we should start.
    Then we can talk about how to do this more specifically.
     
  17. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    this is their problem, not mine. If they want a work from anybody on this site. you can be rude to them. I didn't think it was that hard. I have many shape, triangle wave, sinus wave, square wave and semi-circle wave. If I can make it work as a slider, then it will be finished
     
  18. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    How do you define and draw the lines in code?
    Do you at some point actually have a mathematical function to define the, for example, sinus wave? That's what it sounds like.

    Any given point on a graph can be described by f(x) = y, with f being the function, x being the input and y being the output. So if you plug in some value x into your function, you get the result ("height") at that position. Imagine a simple linear function f(x) = 2x. If we plug in x=1, then f(1) = 2. If we plug in x=2, then f(2) = 4. And so on. Now imagine you plugged in the x coordinate of your mouse relative to the graph. The result would be the y value on the graph for the given x. This means we can combine the x coordinate of the mouse and the result of that function as y, to get a new position (x, f(x)), which is the position at which you would want to draw your circle. Doing this every frame, means the circle will always be on the line, no matter the shape of the line (defined by some arbitrary function f).

    Does this make more sense? If not, where exactly do you have trouble? I still believe you can do this.