x


character controller randomly flies up

Ok so I've got a topdown camera, and a sphere with character controller component and this script attached to the sphere:

    var speed = 5.0;

function Update () {
var controller : CharacterController = GetComponent(CharacterController) ;
var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
var vec = new Vector3(x,0,z);

controller.Move(vec);


}

The sphere is on a flat plane, and theres some static cubes attached to all sides (walls).. at first I thought this happened every time I bumped the sphere into something, but after playing around I realized it's not upon bumping, but upon going backwards (i.e. the direction the sphere is NOT pointing), what happens is that it flies upwards towards the screen. I really dont see how where it's facing can affect its Y access movement as the code clearly only alters the X and Z axis

any idea what's causing this?

extra info: all walls have mesh colliders Tried this again with a cube, it took longer to happen (where with the sphere it happend instantly when i moved backward, with the cube i had to move around alot before i noticed it elevated) There is a script attached the camera that forces the sphere to look at the mouse pointer (surely this is irrelevant when the move script ignores the sphere's direction)

Update: Now i realize that it's not when moving back only, but also right, not sure what this means exactly.

more ▼

asked Mar 01 '10 at 03:52 AM

KrazyKain gravatar image

KrazyKain
6 3 3 9

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

3 answers: sort voted first

Whilst I'm not sure why your controller was heading skyward (y axis)... the problem you have is this:

controller.Move()

I don't know the innards of the Move() function, but basically it seems to be expecting a 'world space' transform vector. So all you need to do is convert the Input.GetAxis() vectors from local to world space like this:

var speed = 5.0;

function Update () {
    var controller : CharacterController = GetComponent(CharacterController) ;
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    var vec = new transform.TransformDirection(Vector3(x,0,z));

    controller.Move(vec);
}

http://unity3d.com/support/documentation/ScriptReference/Transform.TransformDirection.html

more ▼

answered Mar 01 '10 at 11:40 AM

Motionreactor gravatar image

Motionreactor
395 1 5 16

The problem with using transform over Move is that my character then ignore's the mesh collisions and moves right trough them, plus using transformDirection makes it move the direction it's facing, I want it to move up down left right regardless of where its facing. (when I first tested with translate, I used Space.world to accomplish this, but still had the problem where it ignored collisions) Thanks alot for taking the time anyway

Mar 01 '10 at 03:06 PM KrazyKain

Oops, sorry that line was actually meant to have "controller.Move(vec);". I had forgotten to fix that, as I had changed it temporarily to "transform.Translate(vec)" only for comparative testing. So I edited the answer above. It should work fine now.

Mar 01 '10 at 03:20 PM Motionreactor

this still seems to result in the character moving on his own access instead of the world space.... and for some reason still flies upwards when moving back. you can't think of anything outside of the code that might be causing this?

Mar 01 '10 at 03:27 PM KrazyKain

I duplicated your setup by creating a new scene using the standard assets 'first person character controller' prefab and then attached your extra script. It behaved strangely until I made that change, but it never altered the y transform. There must be something awry somewhere else in the project. Perhaps another script is changing the y transform. You could also check: edit > project settings > input. Settings appear in the inspector window.

Mar 01 '10 at 03:33 PM Motionreactor

ok well thanks for confirming its not the script, atleast I can stop trying to fix that and look elsewhere... I'll keep you updated

Mar 01 '10 at 03:53 PM KrazyKain
(comments are locked)
10|3000 characters needed characters left

Check your character controller's gameobject's children for any rogue colliders. It sounds like you might have created a sphere gameobject (gameobject->create other->sphere, which comes with a sphere collider attached) and then attached that to your character controller.

What then happens is your character controller bumps into those rogue colliders, causing it to try to climb over itself... which could explain your Y axis jumps.

You can alternately test this theory out by changing the Step Offset in the Character Controller to 0 and that might fix your jumps as well.

more ▼

answered Apr 25 '10 at 03:09 PM

pyro gravatar image

pyro
760 4 5 23

nice, this fixed my problem AND i like the term "rogue colliders"

Oct 08 '10 at 09:08 PM fucrate

I had the same issue, thanks so much!

Dec 17 '10 at 08:40 PM UltimateBrent

works like a charm!!

Jul 29 '12 at 01:03 AM TMPxyz

@pyro: you, sir, take my like. I was banging my head all day trying hell lot of different things. This resolved my problem. Cheers!

3 days ago fueldown
(comments are locked)
10|3000 characters needed characters left

flyhax.

/thread

more ▼

answered Mar 25 '11 at 10:09 AM

h4wkeye gravatar image

h4wkeye
189 26 30 40

(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:

x1370
x1045
x510

asked: Mar 01 '10 at 03:52 AM

Seen: 3067 times

Last Updated: 3 days ago