Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Topthink · Mar 22, 2018 at 01:26 AM · rigidbodyscript.variable

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.

Comment
Add comment · Show 5
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 Topthink · Mar 22, 2018 at 01:30 AM 0
Share

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.

avatar image Topthink · Mar 22, 2018 at 01:37 AM 0
Share

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.

avatar image Topthink · Mar 22, 2018 at 06:38 PM 0
Share

I was away for a bit but I'm back now.

I'll keep looking at this.

avatar image Vyzier · Mar 22, 2018 at 08:19 PM 0
Share

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.

avatar image Topthink Vyzier · Mar 23, 2018 at 12:28 AM 0
Share

I'm not sure if this is what I want but I will look into this immediately. Thank you in any event.

2 Replies

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

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.

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 Topthink · Mar 23, 2018 at 12:29 AM 0
Share

Thank you for taking the time to help me. I will begin looking at this right away.

avatar image Topthink · Mar 23, 2018 at 01:47 AM 1
Share

Works wonderfully. Thank you for your kind and helpful response.

avatar image
0

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

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 Nomenokes · Mar 22, 2018 at 09:41 PM 0
Share

(this would work if you wanted them to go not just forward)

avatar image Topthink · Mar 23, 2018 at 12:31 AM 0
Share

Thank you as well. I am now looking into all these suggestions. I appreciate that you took the time to assist me.

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

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to limit the rotation of a rigid body? 1 Answer

How does the RigidBody.Transform.rotate method work with degrees at 0? 0 Answers

How do I stop my player from going through the platform? 1 Answer

How do I make a general script that uses an unknown object? 1 Answer

How to check variables from other scripts 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