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 xxbadxkarmaxx13 · Jul 18, 2013 at 05:13 PM · javascriptrigidbodyaddforcerandom spawn

How do I add force on an object(with a rigidbody) that I've randomly instantiated along the "Y" axis?

I know I need to use rigidbody.AddForce, But I'm having trouble finding how. This is what I have, but when I start the game, it spawns the object randomly but doesn't add and force to it. Any help would be greatly appreciated.

 var asteroid : Transform;
 var asteroidSpeed : float = -200.00;
 var spawnAsteroid : boolean = false;
 var ran1 : float;
 var transPos : float;
 
 
 function Update (){
 
  if(spawnAsteroid == true){
  if(Input.GetButtonDown("Jump")){
    var Asteroid : Transform;
  asteroid = Instantiate(asteroid, transform.position, transform.rotation);
  asteroid.rigidbody.AddForce(transform.right * asteroidSpeed);
  
  
  }
  }
 
 
 
 }
 
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 amphoterik · Jul 18, 2013 at 05:13 PM 0
Share

Please fix your code formatting.

avatar image xxbadxkarmaxx13 · Jul 18, 2013 at 05:51 PM 0
Share

Ok, it's fixed. Sorry about that.

2 Replies

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

Answer by IgorAherne · Jul 18, 2013 at 07:02 PM

     var asteroid : GameObject; //you are probably dragging an object into slot on inspector. Use GameObject type variable instead of Transform
 // MAKE SURE that rigid body component in present on the object that was dragged from the inspector on the asteroid variable, since we won't be able to affect it's rigid body from FixedUpdate otherwise.
 
     var newAsteroid : GameObject //you want to instantiate an object later, producing a game object. You want to keep in touch with this one. This is why you need a variable of type GameObject.
     var asteroidSpeed : float = -200.00;
     var spawnAsteroid : boolean = false;
     var ran1 : float;
     var transPos : float;
      
      
     function Update(){
      
      if(spawnAsteroid == true){
         if(Input.GetButtonDown("Jump")){ //GetButton will allow object to get pushed while pressed; you had getButtonDown, which only accelerated object for split second
     
             newAsteroid = Instantiate(asteroid, transform.position, transform.rotation); //instantiate an object and store its data in newAsteroid. //You must keep in mind that newAsteroid only holds information about one object. If Instantiate is called again, the previous object will be re-written with a new one, and will no longer be accessible via this newAsteroid.
            //we are using newAsteroid to add force to the newly instantiated object's rigid body. MAKE SURE that rigid body component in present on the object that was dragged from the inspector on the asteroid variable, since we won't be able to affect it's rigid body from FixedUpdate otherwise.
 
          }
      }
 }
  
 
        function FixedUpdate(){
            if(Input.GetButton("Jump")){ 
                newAsteroid.rigidbody.AddForce(0,asteroidSpeed,0); //add forces only in fixed update. 
            }//this function is called 50 times a second by default, as opposed to Update(), which is called much more times per sec
     }
 
Comment
Add comment · Show 1 · 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 xxbadxkarmaxx13 · Jul 25, 2013 at 03:27 AM 0
Share

Thank you it worked like a charm :D

avatar image
0

Answer by homer_3 · Jul 18, 2013 at 06:05 PM

You've made a local var, Asteroid, which you never use. I've found saving an instantiated game object into the same var it was instantiated from causes weird behavior. spawnAsteriod is also initialized to false, so your instantiate will never get called.

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

18 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

Related Questions

A sphere moved by relativeforce is acting wierd? 2 Answers

rigidbody.AddForce to another object 2 Answers

Rigidbody.Addforce not working in Unity 5.4.1 3 Answers

Declaring myRigidbody 1 Answer

AddForce not working 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