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
1
Question by UncleGus · Nov 04, 2014 at 04:22 AM · rotationvectors

Rotate vector A around vector B until it is perpendicular to vector C

I am creating a custom character controller that uses rigidbody physics to move around. At the moment, I can accurately calculate an acceleration vector that will move the player around, based on their inputs (WASD, shift, control), on the horizontal plane. The problem I am trying to overcome now is how to get them to move up and down slopes realistically. At the moment, the acceleration vector is only horizontal. What I am trying to do is get the normal of the terrain plane that the player is standing on (C), and rotate the acceleration vector (A) around the player's local x axis (B) until it is perpendicular to the normal (C).

I'm pretty sure the solution will involve cross/dot products but if this is a solved problem, there is no point me burning brain cells over it!

Comment
Add comment · Show 2
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 AlwaysSunny · Nov 04, 2014 at 03:41 AM 0
Share

Is this a 2D game, where motion is confined to a single plane?

If so, the slope of a collider beneath a given point can be found using a raycast "down" at the collider. The angle between the hit.normal and the world "up" will give you a meaningful value about your slope, which you can use to select the ideal coefficient of force to achieve the behavior you want.

avatar image robertbu · Nov 04, 2014 at 04:36 AM 0
Share

While I could work out exactly what you ask for, I don't think that is the right solution. Given a forward vector of movement (F) parallel to the characters XZ plane, if you have a slope, I think you want to do this:

     F = Quaternion.FromToRotation(transform.up, slope.normal) * F;

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 04, 2014 at 04:38 AM

Well, that's actually pretty easy ;) Just do those 3 steps:

  • project your acceleration vector onto the (normalized) ground normal.

  • subtract that projected vector from your acceleration vector

  • normalize the result and multiply it with your original length.

Something like that:

 //C#
 Vector3 AlignToGround(Vector3 groundNormal, Vector3 acceleration)
 {
     //groundNormal.Normalize(); // usually the surface normal is already normalized if not this line is needed
     var V = Vector3.Project(acceleration, groundNormal);
     V = acceleration - V;
     return V.normalized * acceleration.magnitude;
 }
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 UncleGus · Nov 23, 2014 at 08:57 PM

Thanks for your answers, here's how I ended up doing it.

         Vector3 velocityRightVector = Vector3.Cross(hit.normal, rb.velocity);
         Vector3 newVelocityVector = -Vector3.Cross(hit.normal, velocityRightVector);
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiating Platforms between two Vectors 1 Answer

Rotating tank barrel on X axis while the parent Turret rotates on Y axis 2 Answers

Vector Alignment and Spacing 1 Answer

Flip over an object (smooth transition) 3 Answers

Problems with the orientation of an gameObject 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