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 fmkle-0 · Apr 09, 2013 at 09:59 PM · rigidbodyspawningthrowing

How to add force to an existing object?

I'm trying to make a script that spawns a ball in front of you so that you can hold it and throw it. I can spawn the ball but I only know how to make it move directly after it spawns. I'm also having trouble getting the ball to stay in front of you, I've tried parenting the ball to the object I used to spawn it in the right position but it just gives me an error. Please help me!

This is what I have so far, all it does is spawn a ball that floats:

 var Ball : Rigidbody;
 var ballNumber : int;
 private var fireEnabled : boolean = true;
 var hold : boolean = false;
 
 function Start()
 
 {
 
 ballNumber = 5;
 
 }
 
 function Update()
 
 {
 
 if(ballNumber > 0 && hold == false)
 
         {
 
         var ballSpawnPoint : GameObject = gameObject.FindWithTag("HoldPosition");
 
         var ballFire = Instantiate(Ball, ballSpawnPoint.transform.position, ballSpawnPoint.transform.rotation);
         
         Ball.rigidbody.useGravity = false;
         
         hold = true;
         
         if(Input.GetKeyDown("e")&& fireEnabled == true)
             {
 
             Ball.rigidbody.useGravity = true;
             rigidbody.findWithTag("pickup").AddForce(Vector3.forward * 2000);
             ballNumber -= 1;
             hold = false;
             fireEnabled = false;
             firePause();
         
             }
         
         }
 }
 
 
 

function firePause()

 {

 yield WaitForSeconds(0.7);

 fireEnabled = true;

 }
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 whebert · Apr 09, 2013 at 10:11 PM 0
Share

What is the error you're getting?

avatar image fmkle-0 · Apr 09, 2013 at 10:23 PM 0
Share

The error I get is if I add the line:

ballFire.parent = ballSpawnPoint;

$$anonymous$$issingFieldException : Field 'UnityEngine.Rigidbody.parent' not found.

There was also another one but I have edited the code a load so I can't show you how I wrote it to get a different error where it refused to parent it because it came from a prefab and it said it would cause file corruption.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Vonni · Apr 09, 2013 at 11:01 PM

  1. You do know that with useGravity off it will still move pretty much on its own if it collides with anything. Maybe use isKinematic instead?

  2. Pretty sure its GameObject.FindWithTag("asd") // To clarify: gameObject is the gameobject you have the script on. GameObject is the class. // You should consider not using tags to find objects like this. Tags are very limiting. I used to use them before, but they are close to useless to me now.

  3. To fix your linking problem. ( .parent ) is a function of the Transform class. Make Ball a GameObject. And use gameObject.transform.parent to use this function. And gameObject.rigidbody to access the rigidbody.

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 fmkle-0 · Apr 14, 2013 at 11:48 PM 0
Share

Right, I will try this as soon as I get a chance, I did mess around with the script a lot without making copies and had a parenting one that didn't work so i'll try your suggestion. Will it work if the script is attached to the player and not the object? (Sorry if i ask anything stupid, i am in fact quite stupid.)

avatar image Vonni · Apr 15, 2013 at 12:34 AM 0
Share

I would have this script on the player.

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

12 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

Related Questions

Avoid overlapping of random spawned prefab 3 Answers

What is the best way to make an AI throw something 2 Answers

My animation is out of sync 0 Answers

moving objects in like in amnesia 8 Answers

Should my ball gameobject have a rigidbody component? 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