Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JakeParrott · Nov 15, 2013 at 08:14 PM · javascriptvariablesontriggerenter

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JakeParrott · Nov 15, 2013 at 08:52 PM 0
Share

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);
 }
 

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
  
 }
 }
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JakeParrott · Nov 15, 2013 at 09:27 PM 0
Share

is this all just one script yes ?

avatar image cmpgk1024 · Nov 15, 2013 at 09:32 PM 0
Share

Add the Speed function to to the same script where you have OnTriggerEnter. Then add Speed(otherObj.gameObject); into your OnTriggerEnter.

avatar image JakeParrott · Nov 15, 2013 at 09:33 PM 0
Share

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

avatar image cmpgk1024 · Nov 15, 2013 at 09:38 PM 0
Share

http://wiki.unity3d.com/index.php/DoubleJumpController may help with double jumping, but it is for a 2d game.

avatar image JakeParrott · Nov 15, 2013 at 09:49 PM 0
Share

Thanks I shall give that site a look :)

Show more comments
avatar image
0

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;


  
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cmpgk1024 · Nov 15, 2013 at 09:07 PM 0
Share

This will not work - you can only access static variables in static methods, and will break PlayerController because Update() is not static.

avatar image Sericet1 · Nov 16, 2013 at 01:25 AM 0
Share

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.

avatar image Sericet1 · Nov 16, 2013 at 01:32 AM 0
Share

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

avatar image cmpgk1024 · Nov 16, 2013 at 02:37 AM 0
Share

Sorry, I wasn't aware that this worked in javascript, as I am used to C#.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges