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 JJ · Mar 29, 2011 at 06:39 AM · raycastdirectionangle

Setting exact Angle of a Raycast instead of using Direction

Hi everyone,

I am starting out using Raycasts but wanted to set a raycast that would check in a specific angle, instead of using the regular Vector3 directions like (Vector3.forward).

So something like:

float angleToCheck = 63f;
if( Physics.Raycast(transform.position, angleToCheck, out hit, 10f )
{
...
} 

Obviously the above won't work as a Vector3 is required, but is there a way to represent the angle I am looking for as a vector3 that the raycast will accept? The angle needs to be relative to my gameobject, not the world.

For more background, this is a 2D game, so objects move along the X and Y axis not the Z (vector3.up or y-axis is the front of my objects).

Thanks!

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

Answer by Jesse Anders · Mar 29, 2011 at 10:32 AM

You can just build a vector corresponding to the angle, e.g. (untested):

direction = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0f);

Depending on what convention you're using for your angles, you might need to swap and/or negate the first two arguments to the Vector3 constructor to get the right results.

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 JJ · Mar 30, 2011 at 03:35 AM 0
Share

Interesting, I'm trying this out but the raycast isn't pointing in the right direction. I think what I am missing is making the angle relative (local) to my gameobject.

So, say I have a gameobject rotated around the z axis at 45degress and I want to create a raycast at 20 degrees (in the z-axis) relative to the rotation of the gameobject. I've been playing with eulerangles but that doesn't seem to get me anywhere. Can I just add the gameobject's transform.position to this new direction?

avatar image Jesse Anders · Mar 30, 2011 at 03:44 AM 0
Share

First, make sure you're perfor$$anonymous$$g any degrees<->radians conversions as needed. Second, you can use the Transform functions TransformPoint(), InverseTransformPoint(), TransformDirection(), and InverseTransformDirection() to transform between local and world space. (TransformDirection() is probably what you want here.)

avatar image JJ · Mar 30, 2011 at 04:47 PM 0
Share

Thanks! I'll keep trying to figure it out, I guess I need to learn some of the basics of vectors, magnitude vectors to get to the bottom of this.

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

No one has followed this question yet.

Related Questions

How to shoot raycast with slight random direction? 1 Answer

Raycast across angle? 3 Answers

How to add slight angle/rotation variation to transform.forward? 5 Answers

Raycast in the wrong way 3 Answers

Generate a random direction within min and max angles 2 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