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 /
avatar image
0
Question by Magmaw · Feb 28, 2020 at 12:50 AM · vector3mathvectorvector math

Change direction of vector based on which way player is facing

Hi,

I have a vector along a surface. Because I derive it from a normal vector, it's always facing in the same direction. I need to change it to go in the direction the player is facing, but I'm having trouble getting it to work.

Here's a picture:

alt text


What I have right now is this (wallVector is the vector along the wall):

 bool wallErrorX = (camera.transform.forward.x >= 0 && wallVector.x <= 0) || (camera.transform.forward.x <= 0 && wallVector.x >= 0);
 bool wallErrorZ = (camera.transform.forward.z >= 0 && wallVector.z <= 0) || (camera.transform.forward.z <= 0 && wallVector.z >= 0);            
 
  if (wallErrorX)
  {
      wallVector.x *= -1;
  }
  if (wallErrorZ)
  {
      wallVector.z *= -1;
   }


This works fine for any vectors at 90 degrees, but not for anything else.

Hopefully someone can help. Thanks!

vector.jpg (172.5 kB)
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 Bunny83 · Feb 28, 2020 at 01:22 AM

You said you derive your "wallVector" from "a normal" but you haven't mentioned how. Since this code is the root of your issue it's the important information we have to know. Also I guess you mean the surface normal of your wall? If that's the case you would simply use Vector3.ProjectOnPlane with your forward vector of your player and the normal vector of your wall.


ProjectOnPlane is not black magic but simple vector math. So I'm just wondering how you currently "derive" your vector from the normal.

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 Magmaw · Feb 28, 2020 at 01:33 AM 0
Share

So I just send a raycast out from the player. If it hits a wall, I use Vector3.Cross(raycasthit.normal, Vector3.up) to get the wallVector.

avatar image Bunny83 Magmaw · Feb 28, 2020 at 01:55 AM 0
Share

Well, that doesn't make much sense since the vector you calculate is purely based on the world up vector and the wall normal. As I said you should simply use ProjectOnPlane.


This will just project your camera's forward vector onto the plane surface: Vector3 fwd = camera.transform.forward; Vector3 normal = raycasthit.normal; Vector3 wallVector = Vector3.ProjectOnPlane(fwd, normal);

Of course in a 3d environment the vector could point along the surface in any direction on that plane. So if the player also looks up or down the vector would be tilted along the wall surface. If you want the vector to be horizontal, just do this

 // [ ... ]
 fwd.y = 0f;
 Vector3 wallVector = Vector3.ProjectOnPlane(fwd, normal);

Note that this gives you a projection of the forward vector onto the wall. So the vector length will vary depending on the angle the user looks at the wall. You might want to normalize the resulting vector depending on your usecase.


Alternatively you can use two Vector3.Cross calls like that:

 Vector3 refVec = Vector3.Cross(normal, fwd);
 if (refVec.sqr$$anonymous$$agnitude < 0.0001f)
     refVec = Vector3.up;
 Vector3 wallVector = Vector3.Cross(refVec, normal);

Again if you want to have the resulting vector always being horizontal, do this:

// [ ... ] refVec.x = refVec.z = 0f; Vector3 wallVector = Vector3.Cross(refVec, normal);

avatar image Magmaw · Feb 28, 2020 at 01:44 AM 0
Share

I just tried ProjectOnPlane, and it seems to work! I just had to clamp the $$anonymous$$imum magnitude of the vector to 1.

Thanks!

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

158 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 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

How to get a vector3 (postion) 1 unit away from another in the direction of a 3rd vector3? 2 Answers

How can I calculate the Vector3 b using Vector 3 a pos with an angle? 1 Answer

Vector math problem 1 Answer

Finding Nearest Object Both Positive And Negative? 1 Answer

Given a vector, how do i generate a random perpendicular vector? 5 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