x


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.

more ▼

asked Jun 22 '12 at 11:14 PM

Hor gravatar image

Hor
17 1 1 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Jun 22 '12 at 11:52 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

First of all, thanks a lot! Im reading and trying to apply youur tips. My 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?

Jun 23 '12 at 12:07 AM Hor

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.

Jun 23 '12 at 12:11 AM Hor

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.

Jun 23 '12 at 12:21 AM Bunny83

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 ;/

Jun 23 '12 at 12:23 AM Hor

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 instead.

Jun 23 '12 at 05:11 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1792
x1367
x673
x461
x198

asked: Jun 22 '12 at 11:14 PM

Seen: 1249 times

Last Updated: Jun 23 '12 at 05:12 PM