#If it’s long you should create a thread

1 messages · Page 1 of 1 (latest)

cold timber
#

Creating a thread just in case

#

this is all the relevant code and the issues I'm stumbling accross

#

this too I suppose

#

you can get the idea of what I'm doing. For some reason, the "grounded" raycast check is just not working no matter what value I set it to. It's incredibly janky and usually just works on a single slope (no matter what value I set.) whatIsGround is set to the correct objects

#

Even if I remove the "grounded" check in the conditions for a jump, it still doesn't allow you to do it consistently

#

The movement of the player sometimes just stops briefly too as of the addition of gravity? It's hard to explain

#

and lastly, the player gets stuck on objects sometimes, which is probably a whole separate issue I have to resolve

inner root
#

Oof this is a lot. Let’s keep it simple first. What’s the issue?

cold timber
#

For context: I'm trying to switch my character movement script from using character controller to using a rigidbody, as I've been recommended to make the switch to have an easier time down the line and generally better movement. But honestly, so far it's been pretty dreadful

inner root
#

Is it the raycast?

cold timber
#

The #1 issue to resolve rn is the grounded check with the raycast

inner root
#

gotcha

cold timber
#

a lot hinges on it and it's performing poorly

#

I've been following this video very closely https://www.youtube.com/watch?v=f473C43s8nE

FIRST PERSON MOVEMENT in 10 MINUTES - Unity Tutorial

In this video I'm going to show you how to code full first person rigidbody movement. You can use this character controller as final movement for your game or build things like dashing, wallrunning or sliding on top of it.

If this tutorial has helped you in any way, I would really appreciate...

▶ Play video
#

set up a lot of things in my code and scene as closely as I could to match it

#

as for the gravity, I touched it up a bit in fear that constantly applying a force downwards would screw up horizontal movement (x and z), so I opted to only have gravity start applying a downwards force (that gets accelerated) if the grounded check is false

inner root
# cold timber

The raycast seems correct… Is it working on flat surfaces?

cold timber
#

nope

inner root
#

interesting

#

what’s the scale of the player?

cold timber
#

capsule height is 2

#

but I've tried all sorts of values for the raycast

inner root
#

and you set the layermask correctly right?

cold timber
#

yes

#

triple checked

#

I even tried it when removing the layer check, same results

#

honestly deliberating scrapping all this and returning to character controller... (although thats gonna bring its own set of problems back)

inner root
#

add this so the raycast is visualized

void Update()
{
   Debug.DrawRay(transform.position, Vector3.down * 1.1f, Color.red);
}```
#

Do you see a line being drawn from the player to the ground?

cold timber
#

not that I can see

inner root
#

hmm weird

cold timber
#

wait what was that about gizmos

inner root
#

Make sure you have gizmos drawing turned on in order to see the line

#

But that was to game view and you were looking at scene view

inner root
cold timber
#

yes

#

btw I was screenshotting those from the scene view when I paused the game

inner root
#

Okay let’s try this

void Update()
{
   Debug.Log($"Raycasting from {transform.position} to {transform.position + Vector3.down * 1.1}");
}```
#

See if the numbers it generates make sense

#

you can spawn a gameobject on those coordinates to see if it intersects the ground. If it does then show the inspector of the ground

cold timber
#

honestly idk what to make of this, it's useful info tho

inner root
#

is it the same position as the player?

cold timber
#

how do I create a cube there?

#

you mean in scene view?

inner root
cold timber
#

approximately

inner root
cold timber
inner root
cold timber
#

you and me both

#

I've consistently had trouble with casts working of all types

#

they just dont wanna work for some reason, or they work half the time

#

Im literally on the ground why dont you work

inner root
cold timber
#

when the game is paused

#

when I unpause it reenables itself

#

anyways yeah, the second I get on a slope it registers

#

otherwise it doesnt even if I put the value at 9999

inner root
cold timber
#

yes

#

when the game is paused the script is disabled

inner root
cold timber
#

hold on thats a good question

#

okay well

#

I tried "transform.position + Vector3.up" instead of just "transform.position"

#

no difference

#

oh wait mb

#

I did it on the debug raycast lol

#

YEP THAT WAS IT

#

thank you

inner root
#

🎉

cold timber
#

alright

#

now onto gravity

cold timber
# cold timber

do you think I overcomplicated the hell out of it here

inner root
# cold timber now onto gravity

Definitely. Gravity should be constant, some people do make it so it’s slightly weaker when jumping upwards and set it to something stronger when the player is falling but it’s pretty uncommon to have something like “gravity acceleration” since gravity is already an acceleration force

cold timber
#

hmm, I found that for some reason when I was applying downwards force it always seemed to move the ridigbody at a constant instead of accelerating it

inner root
#

We can keep it simple first:

void FixedUpdate()
{
   rb.AddForce(Vector3.down * someValue, ForceMode.Acceleration);
}```
Try this for gravity
#

by default unity should have gravity set to 9.81f

cold timber
#

oooooooooh

inner root
#

if it was too floaty, you would up that value to something higher

cold timber
#

ForceMode.Acceleration

#

I was using ForceMode.Force

inner root
#

It’s the same thing but doesn’t account for mass

cold timber
#

strange

inner root
cold timber
#

alright thank you a lot

#

that's a lot smoother

#

and the random jankiness seems to have gone away

#

do you perhaps know of a good way to make the player not "freeze" when running into a wall?

cold timber
#

yeah, as in, when colliding with a wall sideways, as long as the player is holding down the button which makes the capsule collide with the surface, it freezes in place, being unaffected by any force including gravity

inner root
#

you mean like stick to walls

#

gotcha

#

you can add a frictionless physics material to the wall

cold timber
#

is there a way for it to apply universally?

inner root
#

I don’t believe so

cold timber
#

I have quite a lot of walls and intend on having a lot more

#

would be quite annoying having to do that for each and every one

inner root
#

actually I think i see something

#

You can add a collider to the player, that’s slightly wider but shorter

#

and add that physics material onto that collider

cold timber
#

yeah something like that is what I'm looking for

#

how would I ensure that physics material doesn't serve as a bullet vest or the player capsule collider and it don't clip?

inner root
cold timber
#

and how do I ensure the capsule and it don't collide?

inner root
cold timber
#

could I make it a child of the capsule?

inner root
#

Otherwise if not, you can set the player to a layer that doesn’t collide with the “slippery vest”as well

inner root
cold timber
#

alright, and now I just create a sliperry material?

cold timber
#

did I do it right?

inner root
cold timber
#

it doesnt seem to be working

inner root
#

make sure it’s slightly wider than the player

cold timber
#

scale is 1.02 0.88 0

inner root
cold timber
#

adjusted the radius, still isn't working

#

radius is 0.6 now

#

does it need a rigidbody?

inner root
cold timber
#

yes

#

not the capsule, the player

inner root
#

The vest should be a child of the player

cold timber
#

it is

inner root
#

can you see it in the scene view?

#

does it look correct?

cold timber
inner root
#

hmm i guess so then

cold timber
#

maybe provides contacts?

inner root
cold timber
#

ah

#

I didnt have it at minimum minimum

#

It works!

inner root
#

🎉

#

hopefully that was it!

cold timber
#

thank you so much for the help man

inner root
#

Good luck on your game!!