Hey guys, sorry for posting a new post so soon, but I have another problem. I’m writing a player script from scratch. So far I made it walk successfully.
I want to make the player jump, I looked at the sample scripts, searched for functions in the documentation, but no luck.
Oh,I know why the game doesn’t know when the player is on the ground and when he’s not. The collisionflags is only returned by the charactercontroller.move function, but I’m not using it. How would I check if the player is on the ground then ?
I just used the Move func and run onto a problem. The function does not apply gravity to my Player, also my player runs awfully fast.
-How would I apply the gravity to my player now?
-Why does it run so fast?
EDIT: Fixed the fast moving. Found out that this command moves the player by the distance, given per frame, not per second. Had to multiply it with deltaTime.
The gravity was in fact applied in the moveDirection.y When you jump, the component y (axe up/down) is suddenly boosted, to simulate a jump. It is quickly then decreased by little quantity. When it reach 0, your character do not move in the y axis anymore. When it falls under 0, your character falls then.
It runs fast because it is frame dependent, and not time dependant. You must know about FPS (frame per second). A game usually runs at 30, or 60 fps. It means the game is updated 30 or 60 times per second.
In your function Update, you move the CharacterController by 5 times the axis Y and X each time Update is called. If the game runs at 60 frames per second, Update is called 60 times. So, you will run 60 times 5. That’s pretty huge.
Simply multiply the value of move by Time.deltaTime. It is the time elapsed between the previous and the current frame.
To make an image, let’s say you have a cake. If your game runs a 4 frame per second, if you don’t set the Time.deltaTime, when 4 frames will elapse, you would have eaten 4 cakes.
If you multiply by Time.deltaTime (which should be around 0.25), you will eat 0.25 cake per frame.
Thanks, I fixed the fast thing myself. I’m pretty familiar with the framerate dependance thing. I’ve done this for a long time with the previous engine I was working with (Genesis 3D), only there I had to make the variable myself, it wasn’t build in the engine (god it was annoying). I figured that this command moves the object with amount per frame short after I posed my post, but I still can’t figure out that moveDirection.y thing.
Where is it used and how does it work? I mean, the moveDirection is translated in the Move function, but what does moveDirection.y mean?
To put it simply, moveDirection is a variable of type Vector3. It is a set a three floats, x, y, and z. You can access them by writing vector.x, vector.y, or vector.z.
For example, if you have var test : Vector3 (5,10,6); writing test.y will return you 10.
So, moveDirection contains x, y, z informations. They are used to define where the CharacterController is heading.