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 lvlup · Jan 26, 2011 at 05:58 PM · forcechangewaitforsecondsvaluetemporary

Reduce ConstantForce value after specified time. WaitForSeconds()?

heyhey again, another question!

previously, i got help with manipulating the constantforce value on my object, upon entering a trigger. now, i'm trying to go beyond that, and do an additional "function"? i want the object to return to its original force, or "speed" after "x" number of time (maybe 5 seconds).

i have no knowledge of anything c#, but i'm learning fast! i started trying to write my own code to do the above, but after all the errors i was getting in the console, i finally have it running ok, i fixed my own errors.

but my code is not doing what i was trying to achieve! can someone take a look and point me in the right direction? in the easiest "straigh forward english layman words" that i could understand and continue learning?

thanks!

here's my code:

using UnityEngine; using System.Collections;

public class Booster : MonoBehaviour { //private vehicle = GameObject.Find("vehicle"); //obtain a reference to the object with the script attached //private constantForce = vehicle.GetComponent<engine>(); //obtain a reference to the script itself //private currentSpeed = constantForce.force; //copy the value of the life variable to a local variable public GameObject target;

 // Update is called once per frame
 void Update () {

 }

 void OnTriggerEnter () {
     Boost();
     StartCoroutine(Slowdown());
 }

 private void Boost(){
     //Engine e = target.GetComponent&lt;Engine&gt;();
       //You don't need a cast, that the whole point of generics.
       //I don't think you need this either.

       ConstantForce motor = target.GetComponent&lt;ConstantForce&gt;();
       //You needed an instance.
       motor.force = Vector3.forward * 5000;
       //Hard-coded values are generally not good.
  }

 IEnumerator Slowdown(){
     ConstantForce motor = target.GetComponent&lt;ConstantForce&gt;();
     yield return new WaitForSeconds(1);
     motor.force = Vector3.forward * -2500;
 }

}

what is happening now is that, yes, the object does "slowdown" because its being "pushed backwards in the negative value"...but that is the problem, it gets pushed backwards eventually, not just have the speed being reduce, its like it reduces from 10, to 0, then -10 which ends up being reverse!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bob5602 · Jan 26, 2011 at 06:12 PM

You could put in a simple if statement so it only gets pushed backwards if the velocity is greater than 0. Then it won't go negative.

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

No one has followed this question yet.

Related Questions

Value won't change/GUI won't draw 1 Answer

how do i write this: apply a negative force value on my object (to make it slow down its velocity), but i don't want it to go past 0, which ends up making the object travel in reverse. 1 Answer

How to Delay A Ball shooting script 1 Answer

Editor Scripting: How To Detect a Change in Player Settings 2 Answers

The Strength Of A Force in A Certain Direction ? 0 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