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
0
Question by Thran0r · Oct 12, 2015 at 07:46 PM · 2dtopdownlaser

Trying to make a topdown 2d laser

 if (Input.GetMouseButton (1)) {
             RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right);
 
             laser.enabled = true;
             laser.SetVertexCount(2);
             laser.SetPosition(0, new Vector3(0, 0, transform.position.z));
             laser.SetPosition(1, mousePos);
 
             if(hit.collider != null) {
                 eController.eCurHealth -= laserDamage;
             }
         }

This is what i have so far and it points the laser in the correction direction (from the player's position to the mouse's position) but the raycasting doesn't seem to work and it does not collide with the enemy player and destroy them. I am relatively new to unity so I am not sure if I missed anything, any help will be appreciated, thanks.

Comment
Add comment · Show 3
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 Cherno · Oct 12, 2015 at 09:43 PM 0
Share

Tip: Use Debug.DrawRay or Debug.Drawline to see your ray. Use the .point variable of your raycasthit to get the world position your raycast hit (if any).

avatar image Thran0r Cherno · Oct 13, 2015 at 08:12 PM 0
Share

I tried drawing the ray and drawing at a line with the parameters that you suggested and i cant seem to see the lines at all, i made sure it was in the update function but still no line.

avatar image brazmogu Thran0r · Oct 13, 2015 at 08:20 PM 0
Share

Debug draw functions are only visible in the Editor window, so you'll have to have it visible while you test to see it.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by brazmogu · Oct 13, 2015 at 08:30 PM

I'm sincerely not sure what are you doing here. Your raycast is only pointing to the right(0,1), and then you're acctivatin laser, which I think is an Edge Collider? Then what are you using Raycast for?

My suggestion is that you change the direction in the Raycast call by setting the direction to the direction from the player to the enemy using Vector3.Angle, and if you're still having problems I suggest checking if the layer of the enemy is set to accept raycasting, and I'd also suggest that you use the LayerMask parameter in your raycast call.

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 Thran0r · Oct 14, 2015 at 06:43 PM 0
Share

its a 2d scene so and i read somewhere that vector2.right was forward in the direction that the sprite is facing, i want to make the laser shoot out of the player from the position it is at to the in the direction that it is facing, i got the graphics working its just the raycasting that isnt working.

avatar image brazmogu Thran0r · Oct 14, 2015 at 06:49 PM 0
Share

If you're rotating the player transform, you can use transform.right, which is the "right" vector from the point of view of the player.

 RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.right);

avatar image brazmogu Thran0r · Oct 14, 2015 at 07:41 PM 0
Share

Wait, here's another thing I don't get: if you want to make the player shoot in the direction it's facing, then what does the mouse position have to do with that?

avatar image
0

Answer by Eno-Khaon · Oct 14, 2015 at 07:34 PM

its a 2d scene so and i read somewhere that vector2.right was forward in the direction that the sprite is facing

That's a bit of a misconception, really.

While that can be true if your character is a right-facing sprite and you're moving to the right, for any other case, it's not anymore.

That aside, you appear to be using the mouse to create the visual effect of your shot, so why not also use it for your raycast?

If your line renderer (laser) is already displaying correctly when using your "mousePos" variable, you should only need to treat that as a relative direction for your raycast.

 // C#
 
 RaycastHit2D  hit = Physics2D.Raycast(transform.position, (mousePos - transform.position).normalized, (mousePos - transform.position).magnitude);
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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Unity 2D Top-Down Raycast length/distance and direction problem 0 Answers

2D Topdown movement without sliding 2 Answers

2D Top Down Game, 2D or 3D defaults? 1 Answer

2D Topdown Shooter Enemy Knockback 0 Answers

(2d - top down) Characters drifting upon movement,Characters drifting upon movement (2d) 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