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 Siloaman · Dec 14, 2015 at 06:04 PM · raycastquaterniondirection

Quaternion and Direction for DrawRay(Make Object Look At Its Feet, Watch where its going...)

I don't actually know how to see the forums of my own questions... (so please email me at: siloaman@hotmail.ca)

My question is really simple.

I have an Avatar that moves across a floor panel maybe 20x20.

I am able to use Debug.DrawRay to watch his Local Forward Direction using:

straightForward = transform.TransformDirection(Vector3.forward) * 10;

What I need to do is create a second DrawRay function that will look in front of him angled towards the ground like he's watching where he is going.

At the start of the game run, my code was:

  1. straightForward = transform.TransformDirection(Vector3.forward) * 10; (Vector3)

  2. noAngle = straightForward; (Vector3)

  3. spreadAngleDown = Quaternion.AngleAxis(15, new Vector3(1,0,0)); (Quaternion)

  4. newVectorDown = noAngle * spreadAngleDown; (Vector3)

if (Physics.Raycast(transform.position, newVectorDown, 10))
{
Debug.DrawRay(transform.position, newVectorDown, Color.green);
Debug.DrawRay(transform.position, straightForward, Color.cyan);
//Debug.Log("Looking at something :)");
onPlatform = true;
}

This code would essentially say that if the Avatar can see something in front of him.. feel free to move ahead. However, when I coded in Input.GetKey(Space) assume its correct and I (transform.Rotate(0, 90, 0, Space.Self);) the Avatar to his right side.. I ran the same 1-4 steps coding again...

Problem is: my DrawRay that looks directly forward is working, but the angled down DrawRay gets wacky, looking upwards and nowhere and random places.. I think it's because its multiplying the Quaternion angles by a new set of world axis values, not Avatar local values.. can someone please help me so that my angled DrawRay always looks at the ground in front of my Avatar local direction???

Please send extra copy of answer to: siloaman@hotmail.ca

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toromano · Dec 14, 2015 at 07:49 PM

@Siloaman I guess you just need another conversion from local to world:

 Vector3 straightForward = transform.forward; //forward direction vector in global axis.
 Quaternion spreadAngleDown = Quaternion.AngleAxis(15, new Vector3(1,0,0)); 
 Vector3 localDownVector = Vector3.forward * spreadAngleDown;
 Vector3 downVector = transform.TransformDirection(localDownVector); // down direction vector in global axis
 
 if (Physics.Raycast(transform.position, downVector, 10)) 
 { 
 Debug.DrawRay(transform.position, downVector, Color.green); 
 Debug.DrawRay(transform.position, straightForward, Color.cyan); 
 //Debug.Log("Looking at something :)"); 
 onPlatform = true; 
 }
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
avatar image
0

Answer by Siloaman · Dec 15, 2015 at 10:36 PM

Thank you so much!! that totally worked.

Only issue was:

Vector3 localDownVector = Vector3.forward * spreadAngleDown;

but I researched and you have to multiply Quaternion * Vector3

not Vector3 * Quaternion or you get a compiler error :D

Comment
Add comment · Show 1 · 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 toromano · Dec 16, 2015 at 06:43 AM 0
Share

I am glad i helped. I did not use a compiler so i could not see that:) BTW, you should not write your comments as answers. Just a friendly advice :)

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

35 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

Related Questions

Making a portal inspired game - Need help! 0 Answers

Using Quaternion orientation with a direction vector 1 Answer

How can I find direction with known 2 projection angles on xz and yz planes? 2 Answers

Problem with raycasting towards mouse 0 Answers

(Raycasts) C# Why am I shooting myself? 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