Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by carter-carl30 · Feb 24, 2016 at 07:50 PM · androidtexture2ddirectionline

Draw a line in opposite direction of mouse drag

Hi all, I am trying to add a direction indicator to my 2d game.

I drag my projectile backwards and let go to fire.

what I want to do is to have a line or texture that, as you drag the mouse extends out a short distance indicating the shot direction (just a straight line) eg:

mouse drag direction <====== <> ========> line/texture is drawn in this direction, or if mouse dragged diagonal down to the left the line/texture is drawn diagonal up to the right.

any help appriciated, I have a very basic understaning of scripting any help appriciated, thankyou

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

1 Reply

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

Answer by phil_me_up · Feb 24, 2016 at 08:57 PM

A little bit of understanding of Vector maths will help you here.

You'd probably start by recording the position of the mouse when it was initially clicked, let's call it (100,100) for now. Then in every update (or some other sensible interval), check the current position of the mouse. Lets assume the new position is (80,80).

Now, to show where your mouse started and where it is now, simply draw a line between the two points. Perhaps using the Unity Line Renderer will be a good start.

To get the line drawing in the opposite way, first work out the vector which describes the movement from point A to point B. In this case that's (80,80) - (100,100) which will give you (-20,-20). This means that from your original point of (100,100), the mouse has moved -20 in the x axis and -20 in the y axis, giving you the new value of (80,80). To get the opposite of this, simply multiply by -1, which would give you (20,20). Add this to your start position and you get (120,120). Draw a line from the start position to this new location and that's it!

Comment
Add comment · Show 9 · 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 carter-carl30 · Feb 25, 2016 at 08:05 PM 0
Share

Hi phil_me_up, thankyou for your quick answer! I have used a script I found on here for creating a laser and tried to change it.

  #pragma strict
  
  @script RequireComponent (LineRenderer)
  
  var projectile : Transform;
      var mouse : Vector2;
      var hit : RaycastHit;
      var range : float = 5.0;
      var line : LineRenderer;
      var line$$anonymous$$aterial : $$anonymous$$aterial;
      var ray : Ray;
  
      function Start()
      {
         line = GetComponent(LineRenderer);
         line.SetVertexCount(2);
         line.renderer.material = line$$anonymous$$aterial;
         line.SetWidth(0.2f, 0.2f);
      }
  
      function Update()
      {
         ray = Camera.main.ScreenPointToRay(projectile.transform.position);
         if(Physics.Raycast(ray, hit, range))
         {
           if(Input.Get$$anonymous$$ouseButton(0))
           {
            line.enabled = true;
            line.SetPosition(0, projectile.transform.position);
            line.SetPosition(1, projectile.transform.position*-1);
           }
           else
            line.enabled = false;
         }
  
      }

It is drawing a line from my projectile, however it isnt following the angle, see picture attached, could you please assist me further if possible, thanks

alt text

aim-line-help.png (128.5 kB)
avatar image phil_me_up carter-carl30 · Feb 25, 2016 at 09:53 PM 0
Share

This is what draws your line:

line.SetPosition(0, projectile.transform.position); line.SetPosition(1, projectile.transform.position*-1);

It draws from the projectile.transform.position to the inverse of that position. Assu$$anonymous$$g the transform position is (10,10) for example (2D for simplicity), the inverse is that * -1, which is (-10,-10). This means a line will be drawn from (10,10), to (-10,-10). By definition this will be from the projectile position, to the origin point (0,0) and back out the same distance to (-10,-10)

You need to look at those two lines and decide how you can draw (as a starting point), from the point where the mouse was first clicked, to the point where the mouse is now (hint, the mouse position can be read from Input.mousePosition)

Now you have that, how can you take that line and draw it in the opposite direction. (hint, the vector describing one point to another is one vector $$anonymous$$us the other. You then just need to turn that around. If the startPos is (20,20) and the newPos is (10,10), the line you want to draw will go from (20,20) to (30,30) )

Now hopefully you'll have a line that shows where the mouse was first clicked and a line point directly away from the new mouse position.

The final step is to transfer that point to be drawn on your slingshot. If you have a slingshot at (5,5) and a line which goes from (20,20) to (30,30). moving that so it starts at (5,5) is a case of moving both (20,20) and (30,30) by some vector which will make (20,20) (5,5). In this case, subtract (15,15) from both (20,20) and (30,30) and you'll end up with a line from (5,5) to (15,15).

I'm not giving exact answers and code because it's very, very important that you understand vector mathematics when making a game and going through the above piece by piece will really help with that. Hope that's not obstructive but just handing over the code for questions like these doesn't really help anyone!

avatar image carter-carl30 phil_me_up · Feb 26, 2016 at 08:12 PM 0
Share

Im trying hard to understand this, quite confusing as I have a basic understanding of maths and scripting (i'm trying! :) )

am I correct in thinking I need to set up 2 positions. The first input mouse position eg

 function update(){
 p1 = Input.mousePosition;
 }
 
 so when player clicks the projectile it sets p1 as the current mouse position.
 
 and also have a second position that the line draws to from the current mouse position
 
 function update(){
 p1 = Input.mousePosition;
 p2 = Input.mousePosition *-1;  <<<<this been the exact opposite of mouse position
 }
 
 i tried this:
 
  #pragma strict
  
  @script RequireComponent (LineRenderer)
  
  var projectile : Transform;
      var p1 : Vector2;
      var p2 : Vector2;
      var hit : RaycastHit;
      var range : float = 20.0;
      var line : LineRenderer;
      var line$$anonymous$$aterial : $$anonymous$$aterial;
      var ray : Ray;
  
      function Start()
      {
         line = GetComponent(LineRenderer);
         line.SetVertexCount(2);
         line.renderer.material = line$$anonymous$$aterial;
         line.SetWidth(0.2f, 0.2f);
         p1 = Input.mousePosition;
         p2 = p1*-1;
      }
  
      function Update()
      {
         var p1 = Input.mousePosition;
         var p2 = p1*-1; 
         ray = Camera.main.ScreenPointToRay(projectile.transform.position);
         if(Physics.Raycast(ray, hit, range))
         {
           if(Input.Get$$anonymous$$ouseButton(0))
           {
            line.enabled = true;
            line.SetPosition(0, p1);
            //line.SetPosition(0, projectile.transform.position);
            line.SetPosition(1, p2);
            
           }
           else
            line.enabled = false;
         }
  
      }

I get a line that does move but its not connected to my mouse click point and seems to be pivoted in the middle of the screen

Show more comments
avatar image carter-carl30 · Mar 03, 2016 at 12:22 AM 0
Share

thats spot on now :) thankyou very much for all your help and descriptions

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.

Follow this Question

Answers Answers and Comments

52 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Loading images fails on Android for existing Texture2Ds. 1 Answer

Render Android device camera from native pointer 0 Answers

Deployed Android Texture2d image scrambled 0 Answers

create line on a texture 2 Answers

How to save Texture2d to album in android? 1 Answer


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