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 /
This question was closed Dec 14, 2015 at 02:07 AM by AgentGold for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AgentGold · Dec 09, 2015 at 04:14 PM · collision detectionmovement script

Can I change an object's movement to match the angle of a wall it's touching?

Hello to all! I've been struggling with this issue for a few weeks now, so hopefully someone will be able to point me in the right direction to solve it.

I have a player character with a sphere collider and a custom movement script. The object has a Rigidbody for detecting collisions, but I've turned off the Rigidbody's ability to affect position or rotation so I can keep tighter control on it. This unfortunately means I have to reinvent a few wheels, but so far only one thing has really eluded me.

My project is built as a 3D game with what is basically 2D movement along the X and Z plane--the floor is absolutely flat with no grooves or slants. I use a sphere cast to look for walls and obstacles the player shouldn't be allowed to pass through and restrict movement so they stop right in front of it. If the player isn't moving directly toward the wall, I want them to move along the surface of it.

For the most part, the character only has to worry about moving up against flat walls facing the four basic directions. I can handle this well enough if the player tries to move diagonally along them, changing it so only the X or Z movement actually goes through. My trouble is coming up when I have an angled wall that the player wants to move toward.

Let's say I have a wall at a 45 degree angle and the player is trying to move up against its lower right side. What happens currently is that my sphere cast detects the wall and stops the movement short so the player stops when touching the wall. If the player tries to move up from there, nothing will happen because it still detects that the wall is there. They need to explicitly move up and to the right so the sphere cast won't detect the wall in order to continue moving along.

What I want to happen is for the player to move against the wall, moving up and to the right even though their input is only for up. I have some ideas for what to do with their movement speed when this happens that I can figure out, but the big thing I need that I can't figure out is the direction the player should be moving (that is, the direction that's parallel to the surface of the wall they're touching). I've attempted to do a few things with cross products to get the correct direction, but it's really out of my league and I can't tell how close or far I am with this math.

One other thing worth mentioning is that the object the player controls does not rotate. The character model will face different directions just fine, but the actual root object the player is controlling always has a rotation of Quaternion.Identity. (The camera is also at a fixed angle in this game, so this makes it easy to control no matter which way the player is facing.)

Does anyone have any experience with changing direction along a surface like this? If I really really need to learn cross products to make this work then I will, but I'm okay with seeing a solution I don't understand and figuring it out over time by looking at it. If anyone can tell me what to do with this or point me in the right direction, I would really appreciate it!

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

  • Sort: 
avatar image
0
Best Answer

Answer by AgentGold · Dec 14, 2015 at 02:06 AM

After getting some advice, I ended up finding the solution I needed. I'll post it here in case anyone with a similar issue needs to know about it.

In my case, it turns out that every wall I need to worry about uses a Box Collider. This means that it has flat surfaces and I don't need to worry about any curves for this particular issue. When I detect an object in my sphere cast, I can call that object's Transform.forward and Transform.right (and their negatives) to find the four possible directions a surface is facing.

Then I can check the angles between the original direction I want to move and each of the four angles and remove any that are more than 90 (these would be moving against of the intended direction). This should remove two, and I can do another quick sphere cast in the remaining directions looking for the same object the original collision was with--one will most likely collide with it and the other won't. The clear one will be the final direction I'll end up moving.

Just to add a feel for friction, I multiplied the original distance moved by (90 - X) / 90, where X is the angle between the original intended direction and the new direction. The you are to walking straight against a wall, the less distance you'll actually move.

Hopefully this helps someone, because it took me plenty long to come across it!

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 TCROC · Sep 21, 2016 at 10:01 PM 0
Share

Hey. I have the exact same issue. Could you provide an example of your code? I would greatly appreciate it. :)

Follow this Question

Answers Answers and Comments

34 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

Related Questions

Prevent Player from passing through walls 1 Answer

rb. MovePosition slowly enters another Collider 0 Answers

Pick up code issue 1 Answer

onTriggerEnter returns wrong object's attribute 1 Answer

Collision not detected at slow speed 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