- Home /
Change the FirstPersonControll Gravity Variable When Entering a Trigger
Hello,
I am creating a game where you are falling for a very long time and very quickly. I want to make it so that gravity is normal (using the pre-built first-person controller in Unity) until I jump off the edge and fall through a trigger. The trigger will then change the gravity variables and a couple others, like max fall speed. I have been trying to use this code to reference the gravity script:
function OnTriggerEnter(Trigger : Collider) {
if (Trigger.tag == 'GravitySpeed') {
var script: CharacterMotorMovement = Trigger.GetComponent(CharacterMotorMovement);
script.gravity = 1000;
}
}
However, I am not 100% on where to apply this script, or even if it is written correctly. It is not working right now. Can you guys give me a hand?
Thanks so much!
Answer by Josh707 · Mar 10, 2013 at 01:44 AM
You should have a script on the player with functions that set all of those values at once and call it from the trigger. In the script on the player, have variables for the character motor and the desired gravity, and set those in a function.
public class newScript : MonoBehaviour { //Don't know how JS scripts start, just the name
var playerMotor : CharacterMotorMovement
var desiredGravity : float
etc.
public function setThoseValues(){ //Not sure how functions are in JS, but must be public
playerMotor.gravity = desiredGravity;
//Do the rest here
}
}
Then make an empty GameObject with a box collider scaled to the size you want, make it a trigger and call the function on trigger enter.
function OnTriggerEnter(col: Collider){
if(col.tag == 'Player'){
col.GetComponent<newScript>().setThoseValues();
}
}
This makes sense. Unfortunately, when I go in and edit the code to fit my environment, I have about 10 compiling errors, and I can never whittle them all down to 0 to actually apply them to the objects... I haven't had this many before and can't seem to get to a place where they will work. Any ideas?
could you post the part of the (other) script where gravity is used please?
Huh. I really don't know anything at all about javascript, but could you post a bit of the code you wrote? I don't know why you would be getting that many errors.
This is just the Character$$anonymous$$otor Script. I just need to call the Character$$anonymous$$otor$$anonymous$$ovement and change the gravity variable. So far I have tried a ton of things and have had no success.
#pragma strict
#pragma implicit
#pragma downcast
// Does this script currently respond to input?
var canControl : boolean = true;
var useFixedUpdate : boolean = true;
// For the next variables, @System.NonSerialized tells Unity to not serialize the variable or show it in the inspector view.
// Very handy for organization!
// The current global direction we want the character to move in.
@System.NonSerialized
var input$$anonymous$$oveDirection : Vector3 = Vector3.zero;
// Is the jump button held down? We use this interface ins$$anonymous$$d of checking
// for the jump button directly so this script can also be used by AIs.
@System.NonSerialized
var inputJump : boolean = false;
class Character$$anonymous$$otor$$anonymous$$ovement {
// The maximum horizontal speed when moving
var maxForwardSpeed : float = 10.0;
var maxSidewaysSpeed : float = 10.0;
var maxBackwardsSpeed : float = 10.0;
// Curve for multiplying speed based on slope (negative = downwards)
var slopeSpeed$$anonymous$$ultiplier : AnimationCurve = AnimationCurve($$anonymous$$eyframe(-90, 1), $$anonymous$$eyframe(0, 1), $$anonymous$$eyframe(90, 0));
// How fast does the character change speeds? Higher is faster.
var maxGroundAcceleration : float = 30.0;
var maxAirAcceleration : float = 20.0;
// The gravity for the character
var gravity : float = 10.0;
var maxFallSpeed : float = 20.0;
Your answer