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
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);
}
Your answer
Follow this Question
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