Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Tageos · Aug 01, 2015 at 10:47 PM · c#debug.drawline

Debug.drawline?

Hello!

I am making a 2D top down shooter. Right now i am working on the shooting mechanism and decided to go with raycasting. What i want to do is first check so that my ray is going the way i want using Debug.Drawline. I have been trying to figure out how to make a straight line from the fire point to the mouse position for a few hours but i cant figure it out really...

Here is what i have so far:

 using UnityEngine;
 using System.Collections;
 
 public class Winchester : MonoBehaviour {
 
     private Vector3 firePointPosition;
     public Vector3 Mouseposition;
     private GameObject firePointObj;
     private Transform firePoint;
     private float mouseX;
     private float mouseY;
 
 
 void Start () 
     {
     firePointObj = GameObject.FindGameObjectWithTag ("FirePoint");
     firePoint = firePointObj.transform;
     }    
     
     void Update() 
     {
         mouseX = Input.mousePosition.x;
         mouseY = Input.mousePosition.y;
         Mouseposition = Camera.main.WorldToScreenPoint (new Vector3(mouseX, mouseY, -10));
         
         firePointPosition = new Vector3 (firePoint.position.x, firePoint.position.y,0);
 
         Debug.DrawLine(firePointPosition,firePointPosition-Mouseposition);
 }
 }

i now get an infinite line (which is fine) but it follow the mouse movement. Any tips?

Thank you!

/Taegos

Comment
Add comment · Show 5
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 Oribow · Aug 01, 2015 at 11:13 PM 0
Share

Change WorldToScreenPoint to ScreenToWorldPoint

avatar image Oribow · Aug 01, 2015 at 11:16 PM 0
Share

And dont start $$anonymous$$ouseposition with a capital letter. All variable names should start with a lower letter.

avatar image Tageos · Aug 01, 2015 at 11:29 PM 0
Share

Thank you! This made it work only when my player is standing still, very wierd... Its a step on the way though :)

avatar image Oribow · Aug 01, 2015 at 11:37 PM 0
Share

Sorry I thought Debug.DrawLine was a Ray. Change

 Debug.DrawLine(firePointPosition,firePointPosition-$$anonymous$$ouseposition);

to

 Debug.DrawLine(firePointPosition,$$anonymous$$ouseposition);
avatar image Tageos · Aug 02, 2015 at 12:11 AM 0
Share

Oh, it was that just that haha. Thank you :)

1 Reply

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

Answer by Positive7 · Aug 01, 2015 at 11:34 PM

 using UnityEngine;
 using System.Collections;
 
 public class Winchester : MonoBehaviour {
 
     public Vector3 mouseposition;
     void Update() 
     {
         if (Input.GetMouseButton(0)) {
             mouseposition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             mouseposition.z = 10;
             Debug.DrawLine(transform.position, mouseposition, Color.red, 10f, true);
         }
     }
 }
Comment
Add comment · Show 3 · 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 Tageos · Aug 02, 2015 at 12:11 AM 0
Share

wow thank you so much!

avatar image Tageos · Aug 02, 2015 at 12:30 AM 0
Share

What if i wanted the line to have a set pixels of length or infinite length? So that the line follows the mouse direction but continues at a desired length? Is that possible do you think?

avatar image Positive7 · Aug 02, 2015 at 12:40 AM 0
Share

change mouseposition.z = 10; to 100 or whatever length you want. Infinite won't work. For that you will have to use Raycast or maybe Line.Renderer should work too.

 using UnityEngine;
 using System.Collections;
 
 public class Winchester : $$anonymous$$onoBehaviour {
 
     public Vector3 mouseposition;
     public float visibilityTime = 10f;
     void Update() 
     {
         if (Input.Get$$anonymous$$ouseButton(0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if(Physics.Raycast(ray,out hit,$$anonymous$$athf.Infinity)){
             mouseposition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             mouseposition.z = hit.collider.transform.position.z;
                 Debug.DrawLine(transform.position, mouseposition, Color.red, visibilityTime, true);
         }
         }
     }
 }

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

ray transform.position goes wrong 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