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 spraycanmansam · May 01, 2014 at 04:21 AM · vectornormalreflectsign

Get normal direction to use as sign

Hi there,

I need to be able to calculate which way an object should reflect off another object.

On first raycast hit I reflect off the normal and if that's enough to miss the object, great. If it continues to raycast hit the object it will add a set rotation each hit so that the object will definitely steer away from it.

Say for example the object was heading towards a wall rotated at 45 degrees. I need to get a sign (-1 / 1) to apply to the set rotation value (say 30 degrees) so that the object will rotate away from it by 30 degrees, not into it by 30 degrees.

I've tried a few approaches with no luck.

Thanks for your time.

Comment
Add comment · Show 1
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 robertbu · May 01, 2014 at 06:54 AM 0
Share

Have you tried Vector3.Reflect()?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kumo-Kairo · May 01, 2014 at 07:01 AM

Using vector algebra you don't actually need any trigonometrical functions, you can rotate your object without calculating any angles or rotation values. Once you get your reflected vector, you can use it to "rotate" your forward direction towards the desired direction.

Vector algebra

Let me explain that a bit.

First you raycast with your forward direction A and check if there's something to hit. If there is, you reflect your forward direction around normal N and get desired direction B. To make a smooth turn, you need to apply some vector calculations. Every frame find vector C = B - A (you should make sure that vector B is normalized). This vector C will be the ultimate difference between your current forward direction and desired forward direction. So if you add vector C to your forward direction (A), you will be heading exactly at where the vector B points. To make a smooth turn just multiply it, let's say, by 0.4 (the turn speed) and change your current forward vector to it. So it should look like this:

 Vector3 C = B - A; // B and A are Vector3, normalized
 float turnSpeed = 0.4f;
 transform.forward += C * turnSpeed * Time.deltaTime;

The closer your turnSpeed to 1, the faster it will rotate to the desired direction. However, don't try to make your turnSpeed more than 1, it won't make sense.

Cheers.


vectoralgebra.png (15.4 kB)
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

20 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

Related Questions

the projectile reflects 1 time only when it hits something, pls help! 1 Answer

rotation, vector bounce problem 1 Answer

getting the normal vector from each side of a box collider 0 Answers

How do I convert a normal to a scale? 2 Answers

Walljumping and Vector.Reflect 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