# Applying constant force to charcter when it goes inside a trigger

I’ve been searching the web to find an answer to this one for ages.
What I’m trying to do: when my character goes inside a trigger, I want him to go in a certain direction (let’s say up), no matter what direction he moves in (well, he can alter it a bit, but he can’t stop going in the direction of the force, like if someone upturned the gravity), but when he comes out of the trigger, the force will stop (like if someone put the gravity normal again, you would still have the speed you were going in). I don’t want the triggers to move each other, but only to move objects with a rigidbody and the character.
I’m using Character Controller and Character Motor on my character.

http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html

since you are not providing your movement code, I will use the example code:

``````var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
var upwardSpeed : float = 5.0;
var inTriggerZone : boolean;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded  || inTriggerZone) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

//APPLY UPWARD CONSTANT MOVEMENT
if(inTriggerZone)
moveDirection.y = upwardSpeed;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}

function OnTriggerEnter(){
inTriggerZone = true;
}

function OnTriggerExit(){
inTriggerZone = false;
}
``````

this should give you an idea

EDIT: TO ROTATE THE PLAYER… let’s assume the above script is named “CharMove”

add this second script to the same player object:

``````private var target : float;
private var moveScript : CharMove;    // ADJUST SCRIPT NAME HERE
var rotateSpeed : int = 4;

function Start(){
moveScript = GetComponent(CharMove);    // AND HERE
}

function FixedUpdate () {

var up = moveScript.inTriggerZone;

if(up){
if(target < 180){
transform.Rotate(Vector3.right * rotateSpeed );
target += rotateSpeed ;
}
else{
transform.Rotate(-Vector3.right * (target - 180));
target -= (target - 180);
}
}
else{
if(target > 0){
transform.Rotate(-Vector3.right * rotateSpeed );
target -= rotateSpeed ;
}
else{
transform.Rotate(Vector3.right * -target);
target += -target;
}
}
}
``````

Hope this helps!
(there is surely a better way to do this part, or at least this can be improved… but I’m not thinking of it just now… The rotation here will not reset quite perfectly… but it’s pretty close…)