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 jasminalien · Feb 17, 2015 at 01:11 PM · c#speedrunnerboost

create speed booster c#

I'm creating a runner game where the player gets a speed boost after running into a 'boost coin' this is what i have so far, but it's not working. can anyone help me?

public class Booster : Coin {

 public float bonusTime;
 public float bonusGain = 500f;
 private bool picked = false;
 private float timer = 20f;

 void  OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         picked = true;
         GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner += bonusGain;
         Destroy(gameObject);
     }
 }

 void Update()
 {
     if (picked == true)
     {
         timer += Time.deltaTime;
         if (timer >= bonusTime)
         {
             GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner -= bonusGain;
             Destroy(gameObject);
         }
     }
 }

}

Comment
Add comment · Show 4
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 jasminalien · Feb 17, 2015 at 11:59 AM 0
Share

my Player doesn't go faster after running into a boost coin

avatar image Graham-Dunnett ♦♦ · Feb 17, 2015 at 01:14 PM 0
Share

What's speedRunner? What script does it belong to? How does that script use it? Why do you destroy the game object? Won't that stop the Update from happening?

avatar image webe · Feb 17, 2015 at 01:51 PM 0
Share

You should probably use a variable to store ControlRunner in.

 ControlRunner control;
 
 void Start(){
  control = GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner> ();
 }

Then you can just use this code in Update and OnTriggerEnter:

 control.speedRunner -= bonusGain;

That's all I can say without seeing more of your code.

avatar image Glurth · Feb 18, 2015 at 03:27 PM 0
Share

I don't see you setting the bonusTime variable anywhere.

Also, suggest you replace this:

 GameObject.FindGameObjectWithTag("Player").GetComponent<ControlRunner>().speedRunner += bonusGain;

with this:

other.GetComponent< ControlRunner >().speedRunner += bonusGain;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sniper43 · Feb 17, 2015 at 01:34 PM

Your ControlRunner might not properly be registering this.

ASSUMING your ControlRunner script is working correctly (you should post it) the collision isn't being detected. This can happen if you are changing the position of the obejcts around you while using a rigibody, but without further info, I won't delve into that.

Also, if you destroy the coin in the triger event, the entire

  if (picked == true)

loop will not EVER execute.

Comment
Add comment · Show 2 · 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 jasminalien · Feb 18, 2015 at 01:18 PM 0
Share

this is the code for ControlRunner

 public float speedRunner = 2f;
 private Vector3 moveDirection = Vector3.zero;
 public bool grounded = true;
 public float time = 4.0f;


 // Update is called once per frame
 void Update()
 {
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
     {
         transform.position += Vector3.left * speedRunner * Time.deltaTime;
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
     {
         transform.position += Vector3.right * speedRunner * Time.deltaTime;
     }
     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
     {

     }
 }

 void OnCollisionEnter(Collision hit)
 {
     grounded = true;
     // check message upon collition for functionality working of code.
     Debug.Log("I am colliding with something");
 }

}

avatar image Glurth · Feb 18, 2015 at 03:32 PM 0
Share

agree with sniper: you probably want to countdown/time the speed boost in the player script (ControlRunner), since you destroy the coin.

OR, rather than delete the coin on collisoin, make it invisible, and destory only in update, when done with speed boost timer.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Power Up speed and jump boost 2 Answers

How to increase the speed of an object's movement when it enters a trigger? 1 Answer

Problem with speed boost 2 Answers

Adding Speed to onTriggerEntered 1 Answer

Limited Energy Regeneration 3 Answers


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