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 /
  • Help Room /
avatar image
0
Question by Tuskone · Jun 07, 2016 at 11:03 PM · 2d2d-physicsraycasthit2ddrawline

Reduce distance between two Vectors

I'm making a 2D top-down game and I'd like to have an Arc from the player coming so in theory swords swings would do damage in a 45deg in a predefined range (weapon dependent, 1f, 2f so on). But no matter how I try to find anything I can't make it work. I don't want to rotate the player towards the mouse, I just want to have an Arc where the mouse position in the middle.

Either it becomes wonky and the degree from Euler is off, or when normalized it tries to draw a line (with Debug.DrawLine) in regards to Vector3.zero.

This is the latest of my tries:

         var startPos = GetComponent<Renderer>().bounds.center;
         var mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10));
         Quaternion q = Quaternion.Euler(20, 0, 0);
         Quaternion q2 = Quaternion.Euler(-20, 0, 0);
 
         Debug.DrawLine(startPos, mousePos);
         Debug.DrawLine(startPos, q * mousePos, Color.magenta);
         Debug.DrawLine(startPos, q2 * mousePos, Color.green);

If at all possible I would like to have it visualized in Editor mode, but I'm getting to the point of thinking it's either hard to do or impossible (to me)

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
0
Best Answer

Answer by Tuskone · Jun 08, 2016 at 11:04 AM

For anyone looking for something like it, my solution was this code:

     GameObject RayCastAtAngle()
     {
         float startAngle = -45 + _theAngle - _theAngle/2f;
         float endAngle = -45 + _theAngle + _theAngle/2f;
         for (float i = startAngle; i < endAngle; i += _segments)
         {
             Vector3 direction = new Vector3(Mathf.Cos(i*Mathf.Deg2Rad), Mathf.Sin(i*Mathf.Deg2Rad), 0f) * CurrentWeapon.GetWeaponRange();
             Debug.DrawRay(transform.position, transform.TransformDirection(direction));
             RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.TransformDirection(direction), CurrentWeapon.GetWeaponRange(), 1 << 10);
             if (hit.collider && hit.collider.name != "Wall")
             {
                 return hit.collider.gameObject;
             }
         }
         return null;
     }

I had to rotate my sprite in the end, but in theory you could get away with an extra empty game object on your player that is rotating instead the sprite.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycast not detecting bullet 0 Answers

How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers

2D Colliders and Raycast miss collision 0 Answers

Checking for Raycast distances not working as expected. 0 Answers

OnCollisionEnter2D not calling, but objects are colliding. 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