Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Pier_Lanzi · Sep 20, 2021 at 11:50 AM · collisionmovementrigidbodyfpsmovement script

Need help with high speed collisions, sanity dwindling

Hi, I'm trying to build movement for a shooter prototype I'm working on, with mechanics similar to the ones in DUSK or Ultrakill; however, when my player object (that for now is a simple sphere) moves at very high speeds, the collisions stop to work altogether. To prevent this, I used a simple raycasting technique to lower the player's movement near walls, here's the code:

     if (playerDirection != null)
     {
         //cast a ray in the direction of the player's movement, and if it hits something it returns the normal vector of the surface that is hit; that is then scaled and subtracted from the player's direction vector so that the movement on the direction of collision is halted

         Physics.Raycast(raycastOriginObj.transform.position, playerDirection, out hitInfoForSpeed, raycastOriginObj.transform.localScale.x);
         //resizes the normal vector so that it is equal to the player's direction on the axis of collision
         normalCollisionVector = hitInfoForSpeed.normal;
         normalCollisionVector.x = normalCollisionVector.x * playerDirection.x;
         normalCollisionVector.z = normalCollisionVector.z * playerDirection.z;
         Vector3 movementVector = ((playerDirection - normalCollisionVector) * moveSpeed * Time.fixedDeltaTime);

         //move
         rigidBody.MovePosition(transform.position + movementVector);
         
     }

And this works a lot better than the standard rigidbody collision detection, as the player doesn't clip thru corners anymore, however sometimes on certain walls (that are simple boxes, nothing fancy), the player goes straight thru (just random ones too, not like this happens on every wall), and it's driving me insane. I'm using MovePosition() because AddForce() in Velocity mode is just not what I'm looking for and I haven't found any good resources that would help me manipulate it in the desired way. I'm thanking in advance anyone who wants to help me on this, and anyone with the patience to read thru this. Cheers

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 Pier_Lanzi · Sep 20, 2021 at 12:15 PM

Nvm, just solved it For anyone who might run in the same problem, setting the rigidbody's velocity seems to work like a charm; here's the code fix, I also made it so the the moveSpeed depends on a variable that is the base movement speed (exactly like the old moveSpeed worked) multiplied by 3 when the player is sliding on the ground:

     if (playerDirection != null)
     {
         float moveSpeed = baseMoveSpeed * (1 + BoolToInt(slideKey) * 2);
         Physics.Raycast(raycastOriginObj.transform.position, playerDirection, out hitInfoForSpeed, raycastOriginObj.transform.localScale.x - 0.1f);
       //resizes the normal vector so that it is equal to the player's direction on the axis of collision
         normalCollisionVector = hitInfoForSpeed.normal;
         normalCollisionVector.x = normalCollisionVector.x * playerDirection.x;
         normalCollisionVector.z = normalCollisionVector.z * playerDirection.z;
      Vector3 movementVector=((playerDirection - normalCollisionVector)*moveSpeed  * Time.fixedDeltaTime);

       //move
      //save the current velocity on the vertical axis to preserve it, so that jumps still work as normal
     //multiply again by moveSpeed since movementVector has decimal values in its slots(because of time.fixedDeltaTime)
         yspeed = rigidBody.velocity.y;
         rigidBody.velocity=new Vector3 (movementVector.x*moveSpeed, yspeed, movementVector.z*moveSpeed);

     }
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

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

Colliders "offset" and going down too. 0 Answers

How to make rigidbody not effect movement while still using it for collisions? 0 Answers

Move to mouse position, (if start pressing from the character) - HELP 0 Answers

Rigidbody going through Colliders help! 1 Answer

stutter when going next a cube 1 Answer


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