Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Sep 08, 2016 at 11:37 PM by gustavim15 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by gustavim15 · Sep 07, 2016 at 05:45 PM · physicsraycastphysics2dangleraycasthit2d

Is it possible to add Angle to Raycast2D?

I'm using a raycast2D in front of the character, but I want to add some angle to it. Is it possible?

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 Drakonno · Sep 07, 2016 at 06:07 PM 0
Share

I assume, it still should go "front", not depth z-axis. :P

If You want to cast a ray from point A(ax,ay), to point B(bx,by) then just go for another way of describing the point. To be exact:

 x = cos(alpha)*r
 y = sin(alpha)*r, 


   B(cos(alpha)*r, sin(alpha)*r)

Where alpha is your angle in radians ($$anonymous$$athf.Deg2Rad), and r is distance from point A. :)

Tell me if You managed to do it, and then I will change comment to an answer, or provide further explanation and example.

avatar image gustavim15 Drakonno · Sep 07, 2016 at 08:12 PM 0
Share

Hi @Drakonno .... I don't understand how it could be from point A to point B, because o the raycast needs a direction. On my code, I'm using something like this:

 hits = Physics2D.RaycastAll(transform.position, transform.localScale.x * Vector2.right, distance, layerToDetect);


This second parameter is a Vector 2 Direction. How can I do it using what you said?

Thank you for your help, and I'm sorry if I didn't get what you said...

avatar image Drakonno gustavim15 · Sep 08, 2016 at 10:06 PM 0
Share

I'm really sorry for late answer.

Let's think. What will happen, if You jusst set "direction" as Vector2(1,1)? It will shot ray from the starting point, calculate local translation (start + translation, not into global (1,1) position), and draw a ray into that direction, right? So, You can create, using provided by me equations, proper normalized vector.

I will post an proper and tested script. Feel free to copy and apply it to Your object. :)

How to get a proper hit informations is covered in many other tutorials, even from Unity devs. Go check them out for further using a rays.

1 Reply

  • Sort: 
avatar image
3
Best Answer

Answer by Drakonno · Sep 08, 2016 at 10:09 PM

Here You have a full script:

 using UnityEngine;
 using System.Collections;
 
 public class RotateRaycast_Script : MonoBehaviour
 {
     public Vector2 pivotPoint = Vector2.zero;
     public float range = 5.0f;
     public float angle = 45.0f; 
 
     private Vector2 startPoint = Vector2.zero; 
 
     // Use this for initialization
     void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update ()
     {
         startPoint = (Vector2)transform.position + pivotPoint; // Update starting ray point.
 
         // Direct use.
         // Get normalized (of length = 1) distance vector.
         // Vector2 direction = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)).normalized; 
 
         // Using function.
         Vector2 direction = GetDirectionVector2D(angle);
 
         Physics2D.Raycast(startPoint, direction, range); // Shot ray.
 
         // Draw ray. For Debug we have to multiply our direction vector. 
         // Even if there is said Debug.DrawRay(start, dir), not Debug.DrawRay(start, end). Keep that in mind.
         Debug.DrawRay(startPoint, direction * range, Color.red); 
       
     }
 
     public Vector2 GetDirectionVector2D(float angle)
     {
         return new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad)).normalized;
     }
 }
 

Cheers!

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 gustavim15 · Sep 08, 2016 at 11:13 PM 0
Share

Clearer then this is impossible! Thank you man for your time and patience...

avatar image Drakonno gustavim15 · Sep 08, 2016 at 11:20 PM 0
Share

You are welcome. :D

avatar image Zie · Sep 24, 2021 at 12:55 PM 0
Share

Helped me alot, thank you!

Follow this Question

Answers Answers and Comments

99 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

Related Questions

I think my raycast detects destroyed gameobjects 0 Answers

RaycastHit2D ground detection 1 Answer

Three out of four of these raycasts aren't working 0 Answers

Racasthit2d range not working 1 Answer

Need help making a ledge climber. The problem, transform.position always returns to vector( 0, 0) 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