- Home /
How do I Add Speed to Instantiated Object.
I created a class of "Racers" in Script ONE, say 100 racers (or whatever)...this is the "Universe" ... the class contains attributes such as speed, endurance, drag, etc. Works fine, no problems creating or managing the class.
In Script TWO, I create a sub-class in which I randomly select a half dozen or so of the racers in the Universe to compete in a race. No problems selecting the racers from the Universe and no problems creating the sub class.
This is from Script TWO ... notice the last two lines here.
for (int i = 0; i < runnersInRace; i++) // instantiate prefab racer
{
int tempMeasure = (100-runnersInRace * 5) / 2 + 150; // This section centers racers on the track.
GameObject cloneCurrentRacer = Instantiate (CurrentRacer);
cloneCurrentRacer.transform.position = new Vector3(95, 1, tempMeasure - i*5);
pickNewNumber: // This section prevents runners from being picked twice by assigning a "-1" to rest.
int thisPick = Random.Range (0, select.dingy.Count);
if (select.dingy[thisPick].dRest<0) {goto pickNewNumber;}
select.dingy [thisPick].dRest = -1;
racer.Add (new CurrentRace (select.dingy[thisPick].dName, select.dingy[thisPick].dSpeed, select.dingy[thisPick].dDrag, select.dingy[thisPick].dWeight, select.dingy[thisPick].dRest));
cloneCurrentRacer.GetComponent<Rigidbody> ().drag = select.dingy[thisPick].dDrag;
cloneCurrentRacer.GetComponent<Rigidbody> ().mass = select.dingy[thisPick].dWeight;
This all works fine when I get over to Script THREE where I actually do the racing. There is nothing to do over there in regard to drag or weight/mass because all that carries over there like magic.
BUT ... speed is a different matter. I control the speed of the racers in Script THREE by this:
rb = GetComponent<Rigidbody> ();
thrust = mySpeed;
void FixedUpdate ()
{
rb.AddForce(thrust,0,0, ForceMode.Acceleration);
}
How do I most easily get the speed component from Script TWO into my thrust component in Script THREE? There isn't a "speed" component to the Rigidbody. It would be perfect if there was because it would carry forward to here automatically and I would have little to do in that regard. But I'm not aware of any speed component to a Rigidbody. I can assign something to "Thrust" above but it doesn't carry forward from Script TWO automagically like mass and drag do.
I don't want to use a for/next loop in my movement script to cycle through the racers and manually convert the speed to force and then insert it into the "Addforce()" above.
Is there some easy way to do this (as with drag and mass) that I am overlooking?
Thanks in advance for any help.
There is gotta be something I'm overlooking here in the specs or something. I'll be watching videos and looking for info in the meantime.
cloneCurrentRacer.GetComponent ().AddForce (select.dingy [thisPick].dSpeed, 0, 0, Force$$anonymous$$ode.Acceleration);
...
I tried adding lines (like the line immediately above) after the mass/drag above in Script TWO. But if I do that in Script TWO, what do I need to put in Script THREE (the movement script) to get the little racers motivated.
Right now I can put the AddForce(thrust,0,0, Force$$anonymous$$ode.Accelleration) in Script THREE and give all the racer the same speed/thrust and control them by drag and/or mass, but I want speed as a variable as well.
I was away for a bit but I'm back now.
I'll keep looking at this.
Do you mean something like GetComponent<Rigidbody>().velocity.magnitude
? I'm guessing when you talk about speed, you're talking about the scalar version of velocity.
I'm not sure if this is what I want but I will look into this immediately. Thank you in any event.
Answer by Scribe · Mar 22, 2018 at 08:54 PM
To expand on what @Vyzier said, velocity is probably what you want to effect, velocity is a combination of speed and direction. To make a velocity you can do the following:
float speed = 2;
Vector3 direction = Vector3.forward;
Vector3 velocity = speed * direction;
now you can do as usual:
GetComponent<Rigidbody>().velocity = velocity;
and get racing!
Alternatively if you want to actually pass the speed value through you could add a variable or public setter to 'Script THREE' such as:
private float speed;
...
public void SetSpeed(float speed){
this.speed = speed;
}
and then in Script TWO you could do:
cloneCurrentRacer.GetComponent<Script THREE> ().SetSpeed(select.dingy[thisPick].dSpeed);
Hope that helps.
Thank you for taking the time to help me. I will begin looking at this right away.
Works wonderfully. Thank you for your kind and helpful response.
Answer by Nomenokes · Mar 22, 2018 at 09:40 PM
Alternatively, if you wanted them to travel in different directions, you could use something like this with a nice built-in function: rb.velocity = Vector3.ClampMagnitude(rb.velocity,speed);
.
To get the speed of a current racer, you can use rb.velocity.magnitude
.
Although I would recommend making a "racer" script for each racer, you can then individually assign values to each one. That would dive into an idea of object-oriented programming that you may not have done yet
(this would work if you wanted them to go not just forward)
Thank you as well. I am now looking into all these suggestions. I appreciate that you took the time to assist me.