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 /
avatar image
0
Question by Zebbi · Apr 09, 2020 at 09:56 AM · movementvector3normalsanglestrigonometry

Using ProjectOnPlane to slide around adjacent edges

I'm using three raycasts to find the edge directly in front of my characterController capsule and store the normal:

 void Update() 
 { 
 
     forwardRay = transform.position + transform.forward * 0.4f;
     RaycastHit hitInfo = default(RaycastHit);
     var raycast = Physics.Raycast(forwardRay, Vector3.down, out hitInfo, Mathf.Infinity, 1); 
 
     if (raycast)
     {
         edgeVector = Vector3.zero;
     }
     else
     {
         RaycastHit hitInfo1 = default(RaycastHit);
         var raycast1 = Physics.Raycast(transform.position, Vector3.down, out hitInfo1, Mathf.Infinity, 1);
     
     if (raycast1)
     {
         ground = hitInfo1.point;
         RaycastHit hitInfo1 = default(RaycastHit);
         var raycast2 = Physics.Raycast(new Vector3(forwardRay.x, ground.y + -0.02f, forwardRay.z), new Vector3((-transform.forward).x, Of, (-transform.forward).z), out hitInfol, Mathf.Infinity, 1);
 
         if (raycast2)
         {
             edgeVector = hitInfo2.normal;
         }
         }
         }
     }
 }

I then use ProjectOnPlane to ensure the characterController slides along the edge instead of just stopping dead in it's tracks:

 GetComponent<CharacterController>().Move(Vector3.ProjectOnPlane(Quaternion.Euler(transform.eulerangles) * new Vector3(0f, gravity, currentSpeed), edgeVector) * Time.deltatime);

The trouble is, it doesn't handle adjacent corners at all and just falls off, since the raycast is only finding the edge normal directly in front of the capsule. Here you can see the red ray is my forward vector in worldspace, and it fires back from there (pink ray) towards the height of the ground beneath my capsule (blue ray): https://gfycat.com/delectableslipperygosling

Is it possible to find the next normal in the direction I'm moving, or better yet, wrap the ProjectOnPLane vector around the nearest normal in it's projected direction? Otherwise, I'm just doing this the wrong way?

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

Answer by streeetwalker · Apr 09, 2020 at 07:04 PM

@Zebbi, are you rotating the player to face the direction of motion? Because it in your gif doesn't look like the ray is in front of the direction of motion toward the end of the video. If it's not rotating then you need 4 ray casts - one for each face.

But either way, I'm not sure what you want to do when you get to a corner: If you want to keep moving around the corner, then I think you need to detect if any two rays are on two different edges/faces at the same time - indicating the player is in a corner, and then you can rotate the player to face the new direction of motion, or what ever you want to do from there.

Comment
Add comment · Show 10 · 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 Zebbi · Apr 09, 2020 at 07:30 PM 0
Share

The player is facing the direction of motion and the ray rotates with the player.

  • The red ray is essentially forward + position shooting down

  • The blue ray is just the capsule's position shooting down

  • The pink ray originates from the x/z of the red ray and the y of the blue ray, shooting -forward of the capsule.

When the capsule hits the edge and forward motion is still being added, I just want it to stop there. If the capsule is rotated back for forth, it should continue to slide along either edge. Here you can see (https://gfycat.com/violetadolescentfrigatebird) that I'm deliberately sliding around the corner and the capsule slides exactly as intended, but I just need to stop it from going off the edge when it hits the corner from either direction. It just needs to stop and stay still until the capsule is manually rotated away from the corner.

avatar image streeetwalker Zebbi · Apr 09, 2020 at 09:51 PM 0
Share

OK, it seems you need more than one edge detection ray. Your red ray is generated as intended to detect the edge as you slide along an edge. While you are sliding along an edge you need to be casting a ray at a 90 degree angle to that in the direction of motion to deter$$anonymous$$e if you hit another edge - your pink ray doesn't appear to do that, and it isn't really visible in the gif.

If you suddenly find yourself detecting two edges at the same time, you know you are in a corner, and then stop any motion. If you rotate at that point, rather than rotating the edge detection with the object, you need maintain them in a planes perpendicular to the edges you've detected, so you don't lose the edge detection that tells you that the object is in the corner.

The thing is, your sliding along the edge and it appears, as you've indicate, your moving directly into the edge. So the object is not facing the resultant direction of motion, so it kind of confuses the problem. Then when you get into a corner and rotate, it seems your rotating the edge detection and the object loses it, so it no longer knows it's in a corner any more...

avatar image Zebbi streeetwalker · Apr 09, 2020 at 11:11 PM 0
Share

Okay, so I need edge detection on both sides as well as forwards, the problem would be if the character was on a tight strip they wouldn't slide correctly as both sides would detect an edge, and technically that would count as a corner. ProjectOnPlane seems to be giving me the edge sliding effect perfectly, but really it the actual vector needs to be culled to the length of the normal in the direction we're travelling towards, so it can just stop when we get there (0,0,0 the vector until we rotate in a direction that it can move). Is there any other way of calculating the ortho vector perpendicular to the normal we're sliding along that wouldn't just keep going along the normal? Or is there a way of doing the edge detection that would work in all circumstances?

Show more comments

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

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

Acquiring a negative angle? 1 Answer

Dot Product Help 0 Answers

Vector3.Lerp not moving object backwards 0 Answers

How to get the angle to animate the player when running? 0 Answers

How can I make my GameObject rotate this way? 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