- Home /
Ways to create walls without using rigidbody physics in 3D,Ways to limit a player's movement without rigidbody physics in
Hi, I am currently making a top-down 3D bullet hell (inspired by the 9S hacking minigames in Nier:Automata) and my player uses controller input to move around using transform.position. All of my movement scripts work how I want them to. My issue is that due to the nature of transform.position, collision isn't as straightforward as I'd like it to be. I simply want to be able to create walls that will stop the player when they attempt to move towards them. I want to know what my options are, and if there may just be something I'm missing. I'd like to use raycasts, but I'm unsure how to limit the player's movement once the raycast hits one of my walls, as setting the player's position to hit.point will simply allow them to walk through the wall after they hit it the first time. Any help would be greatly appreciated, thanks.,
Answer by PvtPuddles · Feb 03, 2021 at 11:05 PM
It seems like you're mostly there, and you have a couple options
A) Stop the player from moving entirely if the raycast hits a wall. This will stop the player from moving into the wall, but unfortunately will also mean they cannot slide against the wall. Note that the ray needs to be cast before the player moves so that you can cancel their movement, rather than forcing them to go to a specific position.
B) If your walls are straight, then you can figure out basically a line that represents the wall. When the raycast detects the wall, you can halt any movement the player has perpendicular to the wall.
This should give you a good jumping off point, if it isn't enough I can brush up on the maths behind option B to help you figure it out.
I like option B, and (after some research) I have used RaycastHit.normal and Vector3.Angle to figure out the angle between my player and the wall they're facing. However I'm unsure on how to create the line to represent the wall (I imagine the answer to this one is staring me in the face), and how to halt their perpendicular movement to it. I appreciate the pointer tho! I was stuck on this all day today and this is my first time using the forums, thanks!
Use Vector3.ProjectOnPlane
with the hit.normal
to get the perpendicular vector.
Thanks, I got it working somewhat, so far I have taken the hit.normal and added it to my movement script so that the player stops when they face the wall, and that is all working as normal. However I'm not sure how to incorporate ProjectOnPlane, and when you go up to a corner the player can slip through the diagonal between the two walls. I assume the correct application of ProjectOnPlane will fix this?
RaycastHit hit;
Ray ray = new Ray(transform.position, leftJoystickDirection);
if (Physics.Raycast(ray, out hit, rayDistance, walls))
{
Vector3.Angle(hit.normal, transform.position);
Vector3.ProjectOnPlane(hit.point, hit.normal);
if (hit.distance >= 0.1f)
{
ic.leftVertical += hit.normal.z;
ic.leftHorizontal += hit.normal.x;
}
Here is my code so far if that didn't make sense, ic.leftVertical and ic.leftHorizontal stand for the GetAxis("Vertical") and GetAxis("Horizontal"), respectively. Also, as of right now, I am not using the Vector3.Angle code for anything.
Your answer
Follow this Question
Related Questions
How to add platform motion to player controller without parenting? - Mostly working code 1 Answer
Make character not move through walls and other objects 4 Answers
Movement based on collision on a custom mesh 0 Answers
How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer
Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers