- Home /
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.
nope still didnt work for me, my character still flying up or when i look up and run it runs into the air and then SLOWLY falls down...
Answer by Motionreactor · Mar 01, 2010 at 11:40 AM
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
The problem with using transform over $$anonymous$$ove 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
Oops, sorry that line was actually meant to have "controller.$$anonymous$$ove(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.
this still seems to result in the character moving on his own access ins$$anonymous$$d 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?
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.
ok well thanks for confir$$anonymous$$g its not the script, atleast I can stop trying to fix that and look elsewhere... I'll keep you updated
Answer by pyro · Apr 25, 2010 at 03:09 PM
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.
nice, this fixed my problem AND i like the term "rogue colliders"
@pyro: you, sir, take my like. I was banging my head all day trying hell lot of different things. This resolved my problem. Cheers!
Setting Step Offset to 0 in the Character Controller worked! (i.e. the Controller was flying/bounding upwards)
Your answer