I want to make a 3d platformer game, and ive tried to mvoe character in many ways:
Using translate:
this is ok, but i would have to make collisions myself.
CharacterController:
this is ok too, there are collisions, but dont really understand how it works on the inside, so
i would rather not use it if i dont have to.
Using rigidbody:
-setting velocity: i dont think thats the proper way to do it
-setting force: that is not really accurate to program
using rigidbody.moveposition:
-non-kinematic: it works, has collisions, easy and accurate, but it is jaggy, and cant turn on interpolation
-kinematic: it works, doesnt have collision, easy and accurate, interpolation works and its not jaggy now
I decided using translate, so i added a collider. But i read at multiple places that colliders shouldnt be moved without rigidbody, so using translate on player if i want collisions is off, based on that. But even if i go that way, how do i check the collision?
Im totally confused which one to use (i think there isnt any other way), should i just stick to character controller? It really annoys me that i dont see how it works. Is it possible to view its code? I couldnt find it anywhere.
I think the most efficient solution is to use a rigidbody and update the velocity. You can let Unity handle the physics and collisions by giving it a rigidbody and collider.
Interpolating from the current velocity of the rigidbody to whatever the player input vector is can give you smooth movement.
The character controller I made for my current project uses transform.Translate and I check for collisions manually using raycasts. It’s a lot of extra work you don’t need to do unless you’re trying to make something very specific.
Translate directly sets the position of your object. It does not take collisions or physics into account. Use translate if you want to bypass physics and use your own logic to determine what the position should be.
Edit: Also, have you looked at Unity’s new, free 3D example project? That should give you an example to work from.
Do this when the physics engine doesn’t support what you want do, or if you feel like it’s easier to do it your own way for some reason. I think what Magician_Arcana was trying to say is that you may not even need to this at all, depending on what you are trying to do.
I am controlling the palyer by setting the velocity now, but movement is jaggy, and interpolation is not working. Do you know a solution for that?
Also, this is almos tthe same as moving with rigidbody.moveposition, both work, both have gravity, collison, but both are jaggy
also in the unity docs, they write this: “In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don’t set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity.” -Unity - Scripting API: Rigidbody.velocity
So is this really a good way to move the player?
Yeah kdgalla explained it pretty well. The reason I use transform.translate is because Unity’s physics conflicted too much with what I wanted to do. For example if the player was standing on a slope without pressing a direction on the analog stick, the character could sometimes start slowly sliding down the slope. I wanted the player to hold completely still if they was no input. There are workarounds but I didn’t feel any of them were effective or efficient enough.
Also I’m not sure how else you’d move a player around without changing the velocity unless you use addForce.
I think I’ve had that happen before where the movement looks choppy. I think I fixed it by handling movement in fixedUpdate instead of update.
I use fixedUpdate, but since it happens 50 times per second (update tiem is 0.02) it doesnt happen every frame, so i see some jaggy movement, interpolation would fix that, but for whatever reason its not working when i set velocity directly, nor when i use rigidbody.movePosition. With the latter, it works if iset isKinematic to true, but then there is no collision, so its the same as Translate… I really have no idea how it should be done properly.
Also, what you said wit hthe slope, well, i dont want the character to slide downwards either, so maybe i should use translate and implement collision myself too… Could you tell me roughly what methods you used for that?
Kdgalla, i missed your edit, where is that 3d project? Could you send me a link maybe? I dont seem to find it.
Well the issue of sliding down slopes might not be as big an issue for you. The player was mostly still, but the gravity was pulling them down just enough to bother me. It’s been a while so I don’t remember exactly what was happening. I think I was just being very picky with how I wanted my controller to behave lol
Also I think the example project they’re talking about is this:
I haven’t checked it out myself, but it’d probably be helpful to take a look at the character controller in there to see how they handle movement.
Unrealistic behavior is not necessarily a bad thing when it comes to a character controller because in a 3D platformer players will want precise control. Using addforce will probably add a more realistic acceleration/deceleration to your player movement but in a platformer, you often want the opposite. A lot of times you start-off at full speed when you push the joystick and instantly stop as soon as you let the joystick go. You might want to make it a little slippery, but in that case, you may want to control it directly.
The only problem with setting the velocity directly, is that it may cancel out any velocity changes from collisions and external forces that would otherwise affect the player’s movement.
Instead of setting the velocity directly, you might try addForce with velocity mode of VelocityChange. It’s relatively new and I haven’t had the chance to experiment with it yet. I’m not sure if it addresses the problem that I mentioned above.
Kdgalla, do you know how to solve jaggy movement when using fixedUpdate? I described in a previous comment of mine when it happens (with most rigidbody controlled movement, i cant use interpolate)
Not sure what’s causing that. Maybe try setting your ground collider to the “frictionless” physics material? How do apply gravity? Is it still jaggy without it?
In a previous reply, i wrote this: “I use fixedUpdate, but since it happens 50 times per second (update time is 0.02) it doesnt happen every frame,” - Im pretty sure this is what causes jaggy movement. So when refresh rate on screen is 60hz and fps is at least 60, i see 60 frames but physics updates only 50 frames per second. Interpolation is there to solve the problem, but it doesnt work if velocity is set directly or if i use rigidbody.movePosition with isKinematic off. (if i turn it on there is no collision, which brings up another problem)
I checked that project but they use CharacterController to move the player, so i guess i really should be using that… But i feel it like cheating, i dont really know whats going on in there
Just to be sure, you have tried setting the velocity directly with isKinematic off, right? That’s what I assumed you were doing.
Is your camera moving too, or is it stationary? I was working on a project one time where I thought the character was slightly shaky but it it was actually the tight-following camera the whole time.
Yes the way the camera is following the player could also be a factor. I think there are some camera follow scripts included in Unity’s standard assets you could try out.
Also using the character controller isn’t cheating, just an alternative to using a rigidbody. You could still try that. Based on what I read when I searched “character controller vs rigidbody” people seem to think character controllers are better unless you want to make a character controller that’s more heavily physics based.
Personally I’ve mostly used rigidbodies in the past to do movement because I’m used to it. I don’t think it really matters which one you use.
Unity’s character controller is pretty easy to use. The only problem I’ve ever had with the CC is that physics objects will bounce off of it as though it were a static object. If you want your player character to get knocked around by physics objects, you’d have to implement this yourself. Even if you were to go back to the method of setting a rigidbody’s velocity directly, though. you’d still have similar problem, like I mentioned before.