- Home /
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<Engine>();
//You don't need a cast, that the whole point of generics.
//I don't think you need this either.
ConstantForce motor = target.GetComponent<ConstantForce>();
//You needed an instance.
motor.force = Vector3.forward * 500;
//Hard-coded values are generally not good.
}
IEnumerator Slowdown(){
ConstantForce motor = target.GetComponent<ConstantForce>();
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!
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.
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.
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.
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!
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 ;)
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
Follow this Question
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