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 hyperAZ · Oct 20, 2016 at 08:00 AM · movementvector3vector3.project

Player moving up and down hill at different speeds (vector projection)

alt text

Consider this a side view, like in a 2D platformer. In the diagram, the light blue line is the slope of the surface the player is standing on (so it's about 45 degrees). The green arrow is the surface normal of the slope. The dark blue arrow is the player's movement vector walking into the slope, and the red arrow is the resulting vector when the blue vector is projected onto the green vector. (so basically, the red arrow is the final vector that the player will move at on the slope). When he's walking up the slope, all is well and he moves at the appropriate speed, but when he's walking DOWN the slope, he continues to move at the slow speed.

//get the vector that the player is supposed to be moving in. Vector3 movement_vec = new Vector3(0.4f,0,0); //player moves to the right //project player movement onto the surface he's on Vector3 adjusted_movement_vec = Vector3.ProjectOnPlane (movement_vec,hitinfo.normal); //now move the player posvec = transform.position; posvec += adjusted_movement_vec; transform.position = posvec;

I do understand mathematically that this is how it's supposed to be, since the vector we're projecting (blue) onto the surface in both cases (uphill and downhill) is the same magnitude, just pointing in the opposite direction, but I'm trying to determine how one would go about making it so that the player would move faster when walking downhill (whether that be a different approach altogether in Unity, or some other mathematical approach). Thanks! :)

unityforumsquestion.png (13.8 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Steamc0re · Aug 18, 2017 at 02:19 AM

@hyperAZ

 void FixedUpdate()
     {
         RaycastHit hit;    

         if (Physics.SphereCast(transform.position, .25f, Vector3.down, out hit, 3f))
         {
             slope = Vector3.Dot(transform.right, (Vector3.Cross(Vector3.up, hit.normal)));
         }
     }

this gives a slope value between -1 and 1 depending on if you are facing uphill or downhill. I use it as a multiplier to adjust move speed in my game

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 winxalex · Aug 28, 2018 at 05:31 PM

Problem is explained in this video

First you need to find slopeDirection (let assume that always points downhill), then to find how slope influence speed by that how moveDirection is different from slopeDirection. If is full opposite from the slopeDirection value is max negative -1 slope max decelerate your movement, and if is in direction of slope(you are going down) is 1, slope is accelerating your movement.

 slopeDirection=Vector3.Cross(Vector3.up, Vector3.Cross(Vector3.up, hit.normal))
 slope = Vector3.Dot(moveDirection, slopeDirection))

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Limit object position which is moved by accelerometer 0 Answers

MoveTowards inside Coroutine 2 Answers

Smooth motion physics Playmaker 0 Answers

Convert WASD to local rotation 1 Answer

Run animation click/touch to move problem 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