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 blindio · May 25, 2013 at 08:37 PM · raycastquaternionangleeuler

How to raycast using a vector3 and a rotation?

Hello everybody,

What I'm trying to do is to cast a ray, starting from the "feet" of my FPS-character controller, with a certain angle to the ground .

I want to make a "climbing constraint" using the method where if the casted ray encounters an obstacle ahead, then I can constraint the climbing of the character by tweeking the angle of the ray. I think this is standard procedure for what I aim to do.

However I can't figure out how to cast a ray using transform.forward and applying it a rotation in order to obtain the direction for my Physics.Raycast .

I have tried using Quaternion and no luck so far. What I'm curently doing is:

 Quaternion rotation = Quaternion.Euler(0.0f,0.0f,45.0f);
 Debug.DrawRay( collider.bounds.min,rotation * this.transform.forward * 400, Color.red);    



I hope I'm not making this too confusing...

Please help and thanks in advance.

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 robertbu · May 25, 2013 at 09:07 PM

You want Quaternion.AngleAxis():

     Quaternion rotation = Quaternion.AngleAxis (45.0f, transform.right);
     Debug.DrawRay( collider.bounds.min,rotation * this.transform.forward * 4, Color.red); 

Note I'm not sure exactly of the relationship of this ray to your object because your code creates a rotation around the 'z' axis. My visualization is that you want it to angle forward from the middle front of the character controller and go forward. If so, 'transform.right' is the axis you want to rotate around. Or if this is a 2D game and you are using transform.right for forward movement, then the axis would be transform.forward. Note for a 3D game, using the collider.bounds.min will not give you the point you expect for your feet.

Comment
Add comment · Show 2 · 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 blindio · May 29, 2013 at 09:05 PM 0
Share

Worked like you said, thank you kind sir.

I put -45° to have it the right angle, and you were right I needed the transform.right axis for my 3d game. You're also right for the collider.bounds.$$anonymous$$, it is slightly off. I will need to tweek it a little to have it right.

avatar image robertbu · May 29, 2013 at 09:21 PM 0
Share

You could put an empty child game object at the feet, and use it for the position of the raycast.

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

13 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

Related Questions

Raycasting at an offset angle? 4 Answers

how do I limit the angle that a bone can move using quaternions? 2 Answers

Problem with Quaternion.Euler() when rotating object 3 Answers

Rotating an object only a set amount of degrees 1 Answer

How can I multiply the rotation of a mirrored object? 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