Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Launching object in direction of mouse drag

Discussion in 'Physics' started by bugzilla, Jul 13, 2018.

  1. bugzilla


    Dec 9, 2008
    I am trying to make a 3D physics game where you flick balls in the direction of your mouse drag. I have this script but it only seems to work in the vertical direction. Any ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. public class DragToLaunch : MonoBehaviour {
    6.     public float    fireForce;
    7.     public Vector3     mouseStartPosition;
    8.     public Vector3     mouseEndPosition;
    10.     public Vector3    heading;
    11.     public float    distance;
    12.     public Vector3    direction;
    14.     public Rigidbody rb;
    15.     // Use this for initialization
    16.     void Start () {
    17.         rb = this.GetComponent<Rigidbody> ();
    18.     }
    20.     // Update is called once per frame
    21.     void Update () {
    23.     }
    25.     void OnMouseDown()
    26.     {
    27.         Ray vRayStart = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.         mouseStartPosition = vRayStart.origin;
    29.     }
    31.     void OnMouseUp()
    32.     {
    33.         Ray vRayEnd = Camera.main.ScreenPointToRay(Input.mousePosition);
    34.         mouseEndPosition = vRayEnd.origin;
    35.         heading = mouseEndPosition - mouseStartPosition;
    36.         distance = heading.magnitude;
    37.         direction = heading/distance;
    38.         Vector3 dragdirection = new Vector3(0.0f,direction.x,direction.y);
    40.         rb.AddForce(dragdirection * fireForce,ForceMode.Impulse);
    41.     }
    42.     //-------
    43. }
  2. dgoyette


    Jul 1, 2016
    Any reason why you're using ScreenPointToRay, instead of just Input.mousePosition? It seems like you're really only interested in the mouse position during the Down and Up events.

    Is your camera pointing down? Your DragDirection vector also looks a bit odd to me. You're using the `x` component of your direction vector into the `y` component of your force Vector. Is your camera in a weird orientation?
  3. bugzilla


    Dec 9, 2008
    Actually, it was the only code I could find from all the research I did that matched what I was trying to do. I experimented with putting the X and Y coordinates into the X,Y,Z components of the Force vector but so far no luck.

    It seems like a common thing to do in any physics based game so I am surprised there is not sample code floating around for this type of event already.