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 27, 2011 at 10:08 AM · velocityforcewaitforsecondsdelay

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.

can't seem to find answers related to this, there's some questions up but no answers to them.

again, i'm a game artist, starting out to learn to script, so i'm looking for help, and best if i get pointed in the right direction and where to look and what to look at to solve my current problem.

after writing my code (i'm trying! learning alot already, coming from zero c# background), i am now getting this error:

Assets/Booster.cs(34,27): error CS1955: The member `UnityEngine.ConstantForce.force' cannot be used as method or delegatecannot be used as method or delegate

here's what i have so far written:

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 * 500;
       //Hard-coded values are generally not good.
  }

 IEnumerator Slowdown(){
     ConstantForce motor = target.GetComponent&lt;ConstantForce&gt;();
     yield return new WaitForSeconds(5);
     if (motor.force ("0")){
     motor.force = Vector3.forward * -250;
     }
 }

}

my error above is after i tried adding the "if" statement at the Slowdown function. I'm trying to make it check to make sure the object does not get pushed backwards into the "negative velocity" which makes it travel in reverse. like it's going from 10, to 0, then to -10. i don't want it to go down past 0 into "reverse".

the best thing now is if i'm able to write it so that it returns to it's original velocity, prior to "slowdown". like it's traversing at say 1000 before my Boost function, after the Boost function i want to slow it back down to the original speed after a certain period of time.

big thanks to anyone that helps!

Comment
Add comment · Show 2
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 Bunny83 · Jan 27, 2011 at 03:42 PM 0
Share

Can you show me how you move your object forward? Do you use the constantForce to move it, or do you move it "manually" via transform? $$anonymous$$aybe some code what you are doing in your other script that handles the movement.

avatar image nowhereman · Jan 27, 2011 at 10:20 PM 0
Share

That is an obnoxiously long title. Just saying...

1 Reply

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

Answer by Bunny83 · Jan 27, 2011 at 10:22 AM

Just set rigidbody.drag to something greater than 0 that will slow it down. The bigger the value faster it slows down.

edit

Just saw your that you've got an error message.

Assets/Booster.cs(34,27):

In brakets behind the scriptname you can see the linenumber and the column where the error have been spoted.

if (motor.force ("0"))

That won't work because you used the variable like a function. You want to check if the force is 0 but to check a variable you need a compare-operator like == or >= or <=

Next problem: motor.force is a Vector3. That means it has 3 values X,Y and Z. You can't compare a Vector3 with a single number but with another Vector3.

That's all possible:

// check if the force is 0,0,0 if (motor.force == Vector3.zero)

// magnitude calculates the length of the vector which is a single number and can be compared with 0 if (motor.force.magnitude == 0)

But as i mentioned above you should use drag to slow it down. Drag represents friction like forces that gets automatically applied by the physics system.

second edit

Just realised what you want to archive. It seems you use a rigidbody, otherwise ConstantForce wouldn't make any sense. A rigidbody moves with it's velocity. A constant force will apply an acceleration on the rigidbody what will make it moves faster and faster because the velocity increases. I don't know how you move you object "normally". If you want to check the speed / velocity of your object you have to look at rigidbody.velocity. The force property of your constantforce component you tried to check will never change unless you set it to something else.

Can you give us some more information what kind of object we're talking about? A player that moves on the ground? How is the movement implemented? Do you use a CharacterController?

third edit

To give a rigidbody a single immediate boost just do this:

float boostValue = 20; // adjust to your needs

private void Boost() { rigidbody.velocity += transform.forward * boostValue; }

that will increase the velocity by boostValue and the rigidbodys drag bring it back down slowly automatically.

Comment
Add comment · Show 5 · 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 lvlup · Jan 27, 2011 at 11:46 AM 0
Share

heya bunny83 thanks for the great reply, learning already from the little bits you explained for me!

um yes, i am using a rigidbody cube as my object. it's a placeholder for now, but it will be a vehicle of some sort, like a boat or glider. all i needed for this player vehicle is to travel forward, in the Z. and then left and right to switch "lanes" (like a 3d Frogger if you may)..i wrote that in a separate script, called Engine.cs. i applied a constant force component to the rigidbody and thats how i am making my vehicle move.

this boost script is like a special player powerup/bonus.

avatar image lvlup · Jan 27, 2011 at 11:49 AM 0
Share

since its a powerup, the effects of it, i want to wear out after a certain amount of time.

the "boost" is a OnTriggerEnter, so when the player travels into/across it, they get the bonus.

basically i just want to, boost, the player, increase it's original velocity, for a given number of seconds, and then reduce it back down to the speed it was going at before the boost powerup.

not sure if you can see what i'm trying to do with my amateur attempts at scripting.

and maybe you could show me "the right way" to achieve what i'm doing? for learning purposes please.

big thanks!

avatar image lvlup · Jan 27, 2011 at 11:50 AM 0
Share

so no, i don't use a charactercontroller. should i be?

avatar image Bunny83 · Jan 27, 2011 at 03:39 PM 0
Share

No, you don't have to use a charactercontroller ;) if you are happy with the movement of your rigidbody, keep it. Am i right that your "cube" is moving always forward without any userinput forced by the constantForce compnent? If so, you probably set the drag value already? You can give your rigidbody a single (immediate) boost by adding a boostvalue to your rigidbody.velocity. I'll update my answer ;)

avatar image lvlup · Jan 28, 2011 at 07:06 AM 0
Share

heya bunny again!

yap, my object is being "moved" by the constant force component...theres no player input to control whether or not to "go" or "stop" :] didnt need one for this purpose.

i actually really like the approach you're showing me to apply my "boost", using transform ins$$anonymous$$d of the constantforce...gonna go tinker with your example and see how i can apply it! =)!!

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

How to Delay A Ball shooting script 1 Answer

Rigidbody velocity has got different direction than VelocityChange force vector 1 Answer

How to not get velocity by the other objects?,How to not get force by other gameobjects? 0 Answers

yield WaitForTime(1); question 3 Answers

How do I move this rigidbody? 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