- Home /
Sphere + rigidbody + character controller
PS: Im super noob, started yesterday ;(
I read so many things about it, and no one could help me.
Im trying to simple add gravity to an object, AND movement too. I can't do it just adding rigidbody (because of gravity) and Character Controller (because of movement) i don't know, this things just dont stack!
Anyone can help me? I really need to move an object WITH gravity acting on it.
Answer by aldonaletto · Jun 22, 2012 at 11:52 PM
The CharacterController is what you need. If you just want gravity, move it with SimpleMove(speed) - speed is the velocity vector that shows in which horizontal direction to move. speed.y is ignored: SimpleMove takes care of the gravity for you. This is great, but as a bad collateral effect you can't add code to jump (vertical movements are totally under system control). The example script in SimpleMove is all you need to move your character in the old Doom style: AD rotates the character, and WS moves it forward/backward.
If you plan to control vertical movement - to add things like jumping or flying, for instance - use Move(offset) instead (offset is the absolute displacement that the character will move). It's more complex, since you must provide the gravity code. The example in Move provides gravity and jump code, and moves the character to left/rigth/forth/back with the keys ADWS.
These examples work fine (the FPS Tutorial player movement script is basically the Move example, and the enemies are moved with SimpleMove) and are a good starting point to add other features.
NOTE: The only unsolvable problem with the CharacterController is the vertical direction: it's assumed to be the Y axis, and you can't change this (to make the character walk on the walls, for instance).
First of all, thanks a lot! Im reading and trying to apply youur tips. $$anonymous$$y first question is: when a window popup telling that "The sphere collider is already added", should i replace, or just add? There two things will stack or just be a trouble for me?
I guess its a good thing to say that i have a 2D game, i just want to move a ball to left and right, and make him fall if have any hole on the way.
You can't use a rigidbody along with a character controller. a character controller IS already a collider. You shouldn't attach any collider. A character controller is always a capsule.
Use a CharacterController if you want to direcly control your character. You can apply gravity manually. The character controller package that comes with Unity shows how it can be done.
An alternative is to use a rigidbody with a collider. However a rigidbody is a physics object. It can be pushed or influenced by other rigidbodies and should be controlled by adding forces. If you don't like that behaviour, use a CharacterController.
I just realize that my gizmo is in a strange way.
Looking at this screenshot: (http://i46.tinypic.com/2i1o749.jpg), we can see that i have the "x" where it need to stay, but the "Z" is in the "y" location.
I guess this is messing me up.. but i coudnt put my terrain in the way that X be my horizontal line, and Y my vertical.. Danm, i guess im confused ;/
The object in the screenshot is a terrain, and you're seeing its Local axes, which obey the Rotation field - but the terrain completely ignores it, thus the axes may have any orientation independently of the terrain.
If you want an infinite vertical background for a side-scroller XY game, create a skybox with your background image. If you need the background at some specific position, use an Unity plane ins$$anonymous$$d.