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!
1 Answer
1
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?
â dudebot13could you post the part of the (other) script where gravity is used please?
â Seth-BergmanHuh. 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.
â Josh707