Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Moving an object in the X axis

Discussion in 'Scripting' started by idrog, Oct 10, 2016.

  1. idrog

    idrog

    Joined:
    Jul 30, 2016
    Posts:
    83
    Hello all. I have a question that I would like help with if possible. I have a Rigidbody with a constant force moving up and I would like to use the OnMouseDrag functions to move it left to right(Not vertical)? How would I use the Input.GetAxis("Horizontal") function with it so I can use my mouse to move it left to right.(The Y position also has a constant force applied to it as well). C# script would be great if possible. Thanks!

    void OnMouseDrag ()
    {
    moving = Input.GetAxis("Horizontal") * speed;
    }

    is what I currently have but its not moving. I have assigned the two variables values on top of my script. Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,371
    You want to apply a force to the rigidbody instance.

    For instance:

    Code (csharp):
    1.  GetComponent<Rigidbody>.AddForce( Vector3.right * moving);
    Where moving is the value you got above.

    BUT! I don't think "Horizontal" is mapped to the mouse by default... check that first by printing out the values you get from "Horizontal" with the Debug.Log statement.
     
    idrog likes this.
  3. jaybdemented

    jaybdemented

    Joined:
    Sep 2, 2013
    Posts:
    112
    Thier is most likely a better way but off the top of my head i would do this

    Float startpoint;
    Float endpoint;

    Void OnMouseDown()
    Startpoint= mouse.x;

    Void OnMouseUp()
    Endpoint = mouse.x
    If(startpoint > endpoint)
    Moving = 1 * speed;
    Else
    Moving = 1 * speed;


    im sure its full of errors but its a idea.
     
    idrog likes this.
  4. jaybdemented

    jaybdemented

    Joined:
    Sep 2, 2013
    Posts:
    112
    thought about it. This might work better

    Code (CSharp):
    1. Float startpoint;
    2. Float endpoint;
    3.  
    4. Void update()
    5. {
    6.      If(getmousebuttondown(0))
    7.      {
    8.           Startpoint = mouzepoistion.x;
    9.      }
    10. If(input.getmousedown)
    11.      {
    12.             endpoint = mousepoistion.x;
    13.      }
    14. If(input.getmousebuttonup)
    15.      {
    16.           Startpoint = 0;
    17.           Endpoint = 0;
    18.      }
    19.  
    20. if(startpoint > endpoint)
    21.      Moving = 1;
    22. Else
    23.      Moving = -1;
    24.  
    25. Transform.poistion = new vector3( moving * speed, transforn.poistion.y, o);
    26. }
    27. }
    Sorry if its a bit ugly. Tpying on a cheap phone
     
    idrog likes this.
  5. idrog

    idrog

    Joined:
    Jul 30, 2016
    Posts:
    83
    Thanks alot bud! Got it moving but now when I am trying to move it left and right with the mouse button it defaults to the other side. Here is my new code.
    void Update()
    {




    if (Input.GetMouseButtonDown(0))
    {


    rb.AddForce(Vector3.right * 5f, ForceMode2D.Impulse);
    }

    if (Input.GetMouseButtonUp(0)) {

    }
    rb.AddForce(Vector3.right * -5f, ForceMode2D.Impulse);

    }