Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Cryo_Quantom · Oct 11, 2020 at 04:35 PM · raycastunity 2draycasthit2dline renderer

Raycast2D/Line Renderer offset on colliders based on mouse position

Hi all,

I've got a Raycast2D and Line Renderer question.

I have a character with a weapon that creates an aiming laser (Line Renderer) on right click. The X size is based on mouse position but will change when it hits a collider (Raycast2d) to where it registers a hit.

The script accounts for the origin being in the center of the player, thus when the origin is X = 0 the laser behaves as intended, however I need the origin to be from where the weapon is located on the player, X = 0.244 (for reference).

When changing the origin point the laser will will have an offset angle based on where it's hitting the collider and does not have an an angle coherent with mouse position.

How can I factor in the angle offset on collider hit with the mouse position? If that makes sense?

See code below:

 using UnityEngine;
 
 public class Laser : MonoBehaviour
 {
     public Camera cam;
     public LineRenderer lineRenderer;
     public Transform firePoint;
 
     // Start is called before the first frame update
     void Start()
     {
         DisableLaser();
     }
 
     // Update is called once per frame
     void Update()
     {
         int layerMask = 1 << 9;
         layerMask = ~layerMask;
 
         if (Input.GetButtonDown("Fire2"))
         {
             EnableLaser();
         }
 
         if (Input.GetButton("Fire2"))
         {
             UpdateLaser();
         }
 
         if (Input.GetButtonUp("Fire2"))
         {
             DisableLaser();
         }
     }
 
     void EnableLaser()
     {
         lineRenderer.enabled = true;
     }
 
     void UpdateLaser()
     {
         var mousePos = (Vector2)cam.ScreenToWorldPoint(Input.mousePosition);
 
         lineRenderer.SetPosition(0, firePoint.position);
         lineRenderer.SetPosition(1, mousePos);
 
         Vector2 direction = mousePos - (Vector2)transform.position;
         RaycastHit2D hit = Physics2D.Raycast((Vector2)transform.position, direction.normalized, direction.magnitude, layerMask: 9);
 
         if (hit)
         {
             lineRenderer.SetPosition(2, hit.point);
             lineRenderer.SetPosition(3, mousePos);
         }
     }
 
     void DisableLaser()
     {
         lineRenderer.enabled = false;
     }
 }



Any help would be greatly appreciated! Many Thanks!

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 cweber4 · Oct 11, 2020 at 04:56 PM 0
Share

If i understand, you want to draw a line between the player and the resulting raycast object without snapping to its absolute coordinates.

avatar image Cryo_Quantom cweber4 · Oct 12, 2020 at 06:03 AM 0
Share

@cweber4 yes, which I is what I was able to accomplish using the above, however the issue I was having was the offset and angle offset when I change the origin position. I was able to fix this though using the same origin position for both line renderer and raycast.

 Vector2 direction = mousePos - (Vector2)firePoint.position;
 RaycastHit2D hit = Physics2D.Raycast((Vector2)firePoint.position, direction.normalized, direction.magnitude, layer$$anonymous$$ask: 11);

0 Replies

· Add your reply
  • Sort: 

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

197 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 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 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 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

Raycast2D not returning any transform instead keeps returning void. 1 Answer

How to get co-ordinates of end point of Raycast2D ? 1 Answer

Raycasthit2d isnt working. 2 Answers

Raycast 2D not working as i hoped 0 Answers

Raycast not refreshing with changing UI gameObject,pointerCurrentraycast are not updating, making unwanted error in selecting gameobject 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