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 ninjaboynaru · Jul 20, 2012 at 04:40 AM · instantiate

Create a ball then launch it

I am trying to instantiate a ball then launch it. Here is my current scrip


var ball : GameObject;

function Start () {

}

function Update () {

if(Input.GetKeyDown(KeyCode.Space)){

var create : GameObject = Instantiate(ball, transform.position, transform.rotation);

}

}


My problem is I don't know how to launch. Don't understand how I would add force to the ball after it is created.

Comment
Add comment
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

3 Replies

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

Answer by ninjaboynaru · Jul 20, 2012 at 05:04 PM

Thanks guys for all your help but I finally figured it out.

create.rigidbody.AddForce(Vector3(0,0,500));

This did the trick and the ball went forward. I am still curios as why Seth Bergman's answer came up with an error

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 TheCodeMonkey · Jul 20, 2012 at 05:12 PM 0
Share

I was actually on my way to provide the answer you just posted, The reason Seths answer did not work was because he was trying to add to the velocity variable of the transform itself. A Transform component does not have a velocity, it simply holds the rotation position and scale information of a GameObject.

You can see that in the reference link he sent it is adding to the velocity variable of a rigidbody component using the transform components direction.

avatar image Seth-Bergman · Jul 20, 2012 at 10:29 PM 0
Share

yep sorry, I was sloppy tired... rigidbody handles physics, as a rule.. replacing "transform" with "rigidbody" would make that work, careless of me

avatar image
0

Answer by Seth-Bergman · Jul 20, 2012 at 05:48 AM

you can access the game object through the var create. you could add the line:

create.transform.velocity = transform.TransformDirection (Vector3.forward * 10);

as seen in the Script reference HERE

so:

 var ball : GameObject;
 
 function Start () {
 
 }
 
 function Update () {
 
 if(Input.GetKeyDown(KeyCode.Space)){
 
 var create : GameObject = Instantiate(ball, transform.position, transform.rotation);
 //create.transform.velocity = transform.TransformDirection (Vector3.forward * 10);
 create.rigidbody.velocity = transform.TransformDirection (Vector3.forward * 10);
 //fixed careless mistake...
 }
 
 }


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 ninjaboynaru · Jul 20, 2012 at 04:41 PM 0
Share

I tried your script, but an error occurs that says velocity is not a member of transform.

avatar image
0

Answer by sam32x · Jul 20, 2012 at 06:25 AM

put a new script on the ball that says

 function Update () {
 transform.Translate(Vector3.forward * Time.deltaTime * 20);
 }

then the ball will move forwards at 20 units per second. if the ball goes the wrong way then change the forwards to left or up or something. if you want the ball to go faster/slower change the 20 to something else, but if you make it go too fast it wont collide properly with other objects.

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 sam32x · Jul 20, 2012 at 06:28 AM 0
Share

the guy above me has a better way though, because my way means it keeps moving forever.

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

7 People are following this question.

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

Related Questions

Instantiated object don't recognize MouseClick. 0 Answers

Instantiate A Prefab 1 Answer

Error when trying to instantiate 1 Answer

Checking if object intersects? 1 Answer

Accessing Script on instated prefab 1 Answer


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