• 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 PotatoInsertion · Jul 03, 2014 at 09:00 AM · trajectorymouse-drag

Get direction of mouse drag so something can be fired along it

Hi all. No code here as I don't even know where to start on this one, sorry.

Essentially, I've a game idea where you can fire things from clouds. You have a cloud, which is stationary, and you drag along the cloud and then release to flick lighting bolts out of it. Like a reverse angry birds; you drag towards the direction you want to fire it.

I imagine the basics would be to detect the click, detect the line it's being dragged along and then once the mouse is released, instantiate an object that then moves along that line. But how would I go about getting that line, ie, the direction the mouse has been dragged just before the mousebutton has been released?

It's for a 2D game, which should make it easier.

Comment
Add comment · Show 2
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 HarshadK · Jul 03, 2014 at 09:16 AM 1
Share

When the mouse goes down you get the position of mouse (start position) and then when user releases the mouse you get position of mouse (end position). Using these start position and end position you can calculate direction and distance as explained in Direction and Distance from One Object to Another.

avatar image PotatoInsertion · Jul 03, 2014 at 01:18 PM 0
Share

Thanks Harshad$$anonymous$$, that's all I needed to get me going. Never would have found that link by myself. Got it working now, will post the code as an answer so I can close the question.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by PotatoInsertion · Jul 03, 2014 at 01:21 PM

Thanks to HarshadK I figured it out. My final code is posted below. I could probably make most of the variables member variables too, but this should be enough to help out anyone else.

 using UnityEngine;
 using System.Collections;
 
 public class GodInput : MonoBehaviour {
 
     public float    fireForce;
 
     public bool     mousePressed;
     
     public Vector3     mouseStartPosition;
     public Vector3     mouseEndPosition;
 
     public Vector3    heading;
     public float    distance;
     public Vector3    direction;
 
     public GameObject    prefab;
 
     void Update () {
 
         if (Input.GetMouseButtonDown(0)){
 
             mousePressed = true;
 
             Ray vRayStart = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             mouseStartPosition = vRayStart.origin;
 
         }
 
         if (Input.GetMouseButtonUp(0)){
 
             if (mousePressed) {
 
                 Ray vRayEnd = Camera.main.ScreenPointToRay(Input.mousePosition);
 
                 mouseEndPosition = vRayEnd.origin;
 
                 heading = mouseEndPosition - mouseStartPosition;
                 distance = heading.magnitude;
                 direction = heading/distance;
 
                 GameObject fireInstance = Instantiate(prefab, mouseEndPosition, Quaternion.identity) as GameObject;
 
                 Vector2 direction2D = new Vector2(direction.x,direction.y);
 
                 fireInstance.rigidbody2D.AddForce(direction2D * fireForce);
 
                 mousePressed = false;
             }
 
         }
     }
 }
 
Comment
Add comment · 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
0

Answer by Max_power1965 · May 10, 2017 at 12:40 PM

Hi guys, I wrote this simple tutorial wich is basically 7 lines of code and works like a charm. Easy and simple. You just have to use te build in Unity event system instead to write long and useless code. No need to use an update or fixed update.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trajectory on mouse up 0 Answers

Right click drag and drop script 1 Answer

Tracking slow mouse movements 1 Answer

Move rigidbody2D with AddForceAtPosition relative to mouseclick (no snapping) 1 Answer

How to make a physical rope which is on drag move and rotate? 0 Answers


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