- Home /
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. Can anyone help? Please. I'm using Character Controller and Character Motor on my character.
Answer by Seth-Bergman · Nov 08, 2012 at 01:49 PM
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..)
It doesn't work :( It finds out that it's ina trigger but it doesn't move :(
are you using another movement script?
This example is a modification of the script found at the link at the top of my answer.
IF that is the movement script you attach to your player, it will work.. CLEARLY, since my code is identical to the jump code already contained therein..
Just tried the script. It works, now only I have to implement the part into my Character$$anonymous$$otor :D Btw, could you make it to work ins$$anonymous$$d pushing the player upward that it would change the gravity so that it would be upside-down?
Unfortunately I don't know the first thing about Character$$anonymous$$otor, I will need to look into that!
Now, if you mean just the player would be upside down (like so they can walk on the ceiling, for example), you would simply need to rotate the player.. There may be a simpler way than $$anonymous$$e, but I will update my answer to show you a way..
Also, if this or any other answer is satisfactory to you, please accept it by clicking the check mark, thanks!
(answer updated)
If you actually want to change the gravity for all rigidbodies as well, that should be easy enough, just give Physics.gravity a positive y value:
http://docs.unity3d.com/Documentation/ScriptReference/Physics-gravity.html
$$anonymous$$an, it's like AL$$anonymous$$OST. The problem is that sometimes it rotates the camera into wierd angles and you can't walk on the ceiling :( But ins$$anonymous$$d of that, it's perfect! :D
Your answer
Follow this Question
Related Questions
Can't use Rigidbody or Character Controller 0 Answers
Stop objects from going through each other with rigidbodies 1 Answer
How Character controller can push another character controller? 0 Answers
Question about pushing objects, "Animate Physics" and Rigidbodies 0 Answers
Which is more demanding on a computer, lots of CCcharacters or lots of Rigidcharacters? 1 Answer