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 clovergruff · Aug 27, 2012 at 12:56 PM · velocitycharacter controller

Restricting character controllers velocity after hitting a wall

Hello! I've been working on my project for quite some time now, and I've been struggling with one specific problem ever since I started working on the platforming part of my game. Since its a third person action game, jumping is frequent, so after some time I realised a little problem.

When my character collides with an object, it doesn't effect the velocity vector of my character at all - this is not a problem when running around on the ground, but since in mid air my character is much less controlable, it is quite hard to miss this problem.

Basically, let's say the character collides with an object by jumping forwards it, then loses the contact with it at some point (Rises above it, or moves beyond the corner of the object), the character then continues its movement forward as if it hadn't hit anything. This is a huge problem, because it makes the platforming incredibly annoying and just not fun at all.

What I'd like is to effect my character's velocity in the X and Z axes, so upon collision my character would lose some of its velocity in the right axis, and just appear to be sliding along the wall, but after it loses the contact, it should build up the velocity again.

I did find a "Sort of working" solution here:

 if (controller.collisionFlags & CollisionFlags.Sides)
 {
     moveDirection.x = GetComponent(CharacterController).velocity.x;
     moveDirection.z = GetComponent(CharacterController).velocity.z;
 }

This code by theory works perfectly, and gets the job dune, but unfortunately it occasionally results in some wierd collision errors, which can make the character pass through solid geometry. I'm pretty sure I'm supposed to use normal vectors and create a costum velocity for my character, instead, but I'm not exactly sure how to do this.

Maybe anyone has an idea and is willing to help out?

Thanks!

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
1

Answer by clovergruff · Aug 27, 2012 at 07:04 PM

After running through some of my old code and putting in some more thought on this, I figured out some issues and eventually came up with the following code:

 function OnControllerColliderHit (hit : ControllerColliderHit)
 {    
     if (controller.collisionFlags & CollisionFlags.Sides)
     {        
         if (Vector3.Dot( hit.normal, moveDirection ) < 0)
         { moveDirection -= hit.normal * Vector3.Dot( hit.normal, moveDirection ); }
     }
 }

It looks like I managed to answer my own question and everything seems to work just fine now. I hope this will help others with a similar problem as well!

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 Zamaroht · Jul 10, 2016 at 03:39 AM 0
Share

Works like a charm! <3

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to tell if the player is turning? 2 Answers

How does the premade FPSController handle character physics? 1 Answer

[Closed] Find the velocity of the Character Controller 1 Answer

HOW DO I MAKE MY CHARACTER GAIN SPEED GRADUALLY 3D 2 Answers

Character Controller Velocity Control 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