• 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
Question by Zebbi · Apr 08, 2020 at 10:44 AM · movementraycastvector3anglecharacter controller

Wall sliding with characterController against an edge

I'm trying to figure out how to make characterController slide along an edge as it would slide along a wall. I'm casting a ray starting forward (a little scaled back so the character can get closer to the edge) and shooting straight down:

 forwardRay = transform.position + transform.forward * 0.4f;
     RaycastHit hitInfo = default(RaycastHit);
     var raycast = Physics.Raycast(forwardRay, Vector3.down, out hitInfo, Mathf.Infinity, 1); 

Then, I find the floor point directly below the capsule:

 RaycastHit hitInfo1 = default(RaycastHit);
         var raycastl = Physics.Raycast(transform.position, Vector3.down, out hitInfo1, Mathf.Infinity, 1);

Finally, I fire a ray back from the point forward where there is no ground, but at the height of the point where there is ground beneath the player:

 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, 0f, (-transform.forward).z), out hitInfo1, Mathf.Infinity, 1);

This gives me the a ray at the edge of where my capsule is trying to move: https://gfycat.com/costlyblushingdragonfly

I then take the point of where this ray hits, subtracted from my forwardRay and multiplied by my speed to give me an edge vector:

 edgeVector = (forwardRay - hitInfo2.point) * Mathf.Abs(currentSpeed) * 10f;

that I can subtract from my final movement:

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

Here's the full code I'm using to get the edge:

 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 raycastl = 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((-forwardRay).x, 0f, (-forwardRay).z), out hitInfo1, Mathf.Infinity, 1);
  
         if (raycast2)
         {
             edgeVector = (forwardRay - hitInfo2.point) * Mathf.Abs(currentSpeed) * 10f;
         }
         }
         }
     }
 }

The issue I'm having now is that it works when everything starts at 0,0,0: https://gfycat.com/unselfishdevotedbird

But totally breaks when the capsule and floor is moved. This is with the floor and capsule moved along the x and z axis by 2: https://gfycat.com/envioustornafricanelephant

And by the time it gets to 9 on the x and z axis, the capsule just falls off: https://gfycat.com/impeccableinferiorfantail

I know this is because it's casting in the wrong direction, but when I correct it by casting directly backwards:

 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);

the wall sliding effect breaks: https://gfycat.com/greentestyirukandjijellyfish

So, obviously all along the wall sliding effect was an accident, but I need to figure out either adjust my charactercontroller movement to slide against the correct vector, or find a relative vector direction I can use that would give the same effect.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

311 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Raycast under crosshair 0 Answers

Acquiring a negative angle? 1 Answer

Find a point in space using Vector3 angles and Raycast 1 Answer

How to move object in 45/30/15 degree arch. 2 Answers

Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 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