Applying force to character inside trigger

I'm trying to create an area which has a wind which pushes my character backwards, without using a rigidbody (as I've heard this can cause issues). I've been using the answer from a basically identical question found on this site here:

http://answers.unity3d.com/questions/26798/adding-constant-force-to-a-charactercontroller-using-the-thirdpersoncontroller-sc

However, the answer was written with C#, and I'm trying to keep all of my code in javascript. I tried converting it, but I've obviously made an error somewhere due my very limited understanding of C#. Heres the script as it stands in my project.

var direction : Vector3;
var speed = 2.0;
var ctrl : CharacterController;
direction = Vector3(0,1,0);

function OnTriggerStay (other : Collider) {
        ctrl = other.gameObject.GetComponent(CharacterController);
        if (ctrl) {
            ctrl.SimpleMove(direction * speed);
}
}

I may have oversimplified somewhere. As is, my code doesn't work at all. Where am I going wrong?

P.S, sorry for butchering someones much more elegant code XD

My problem has turned out to be that my character is restricted from moving in the Z axis. I can't seem to find a way to allow this movement (i'm not even sure where it's restricted). Anyone have any ideas?

2 Answers

2

The direction you set will have it going upwards, not backwards. Try applying the force to the Z axis.

direction = Vector3(0, 0, 1)

Yeah, I only tried setting to that because Vector3(0,0,-1) which was what I had before wasn't working.

The code works fine once I set it to move the character in the x axis. Something is restricting movement in the z axis.