- Home /
Speed Pill... Variable referencing and adjusting temporarily
Hello good people!
In the platform game that I'm creating I have what I'm calling a speed pill which will temporarily boost the speed of the character I have for 5 seconds
What I need to do is on the speed pill item apply a script with an OnTriggerEnter function that adjusts the speed variable in the script called PlayerController to 20 for 5 seconds then it returns to 10. My problem is that I don't know how to do that, as I don't know how to refer to a variable in another script as my programming skills aren't so good.
So far I have the following -
function OnTriggerEnter(otherObj: Collider)
{
if (otherObj.tag == "Player")
{
//here needs to be a function that adjusts the speed
//variable in PlayerController from 10 to 20 for 5 seconds
}
}
Your help is greatly appreciated, Thank you
Also here is the PlayerContoller script if any adjustments need to be made to it
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var dead = false;
private var moveDirection : Vector3 = Vector3.zero;
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded)
{
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
Answer by cmpgk1024 · Nov 15, 2013 at 08:59 PM
function Speed(go:GameObject){
go.GetComponent(PlayerController).speed = 20;
yield WaitForSeconds(5f);
go.gameObject.GetComponent(PlayerController).speed = 20;
}
then call it when you get the collision:
function OnTriggerEnter(otherObj: Collider)
{
if (otherObj.tag == "Player")
{
Speed(otherObj.gameObject);
}
}
Add the Speed function to to the same script where you have OnTriggerEnter. Then add Speed(otherObj.gameObject);
into your OnTriggerEnter.
just tested this and it works perfectly... thank you ever so much. you are a champ! :)
Dont suppose you could point me in the right direction of how to get my character double jumping and climbing? those are my next steps
http://wiki.unity3d.com/index.php/DoubleJumpController may help with double jumping, but it is for a 2d game.
Answer by Sericet1 · Nov 15, 2013 at 09:05 PM
Here is a solution but it isn't the best way to do it.
function OnTriggerEnter(otherObj: Collider) { if (otherObj.tag == "Player") {
PillEffect();
}
}
function PillEffect()
{
PlayerController.speed = 20;
yield WaitForSeconds(5); //Causes this fucntion to wait for 5 seconds
PlayerController.speed = 10;
}
In your PlayerControllerScript put the word Static public in front of var speed; This will allow you access this variable and change it.
static public var speed;
This will not work - you can only access static variables in static methods, and will break PlayerController because Update() is not static.
I pretty sure your wrong about that. I am able access static variables in any function regardless of whether it is a static function or not.
Script A
public class ScriptA : $$anonymous$$onoBehaviour
{
static public int speedA;
void Awake()
{
speedA = 20;
}
}
Script B
public class ScriptB : $$anonymous$$onoBehaviour
{
private int speedB;
void Update()
{
speedB = ScriptA.speedA;
print (speedB);
}
}
This works for me. I am able to access "speed" from ScriptA inside the update function in ScriptB. This is what I was trying to explain in my answer so could you please fix the down-vote
Sorry, I wasn't aware that this worked in javascript, as I am used to C#.
Answer by panbake · Nov 15, 2013 at 10:40 PM
You'll want to use Coroutines to do this. Unity Coroutines
Coroutines allow you to execute a bit of code and then yield (wait) for an amount of time before continuing execution. I am not so familiar with unityscript, so this might not compile but it would look something like this
function SpeedUp(float time)
{
var player = otherObj.GetComponent.<PlayerController>();
player.speed = 20;
yield return new WaitForSeconds(time);
player.speed = 10;
}
Note that if you were to hit a second speed pill before the 5 seconds was up it would not further increase your speed. This may or may not be what you want to happen in this situation.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Detect OnTriggerEnter with tags 0 Answers
Int and Javascript help 2 Answers
Integer in if statement, scripting help 1 Answer
What variables can i declare? 1 Answer