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 Sweex · Jan 17, 2014 at 05:49 PM · c#animationspawnspeed

How to give a speed to random spawned object?

Hi, how can I give a speed to random spawned object. I have this script:

 using UnityEngine;
 using System.Collections;
 
 public class Spawner : MonoBehaviour
 {
     public float spawnTime = 5f;        // The amount of time between each spawn.
     public float spawnDelay = 3f;        // The amount of time before spawning starts.
     public GameObject[] baloon2;        // Array of enemy prefabs.
 
 
 void Start ()
     {
         // Start calling the Spawn function repeatedly after a delay .
         InvokeRepeating("Spawn", spawnDelay, spawnTime);
     }
     
     
     void Spawn ()
     {
         // Instantiate a random enemy.
         int enemyIndex = Random.Range(0, baloon2.Length);
         Instantiate(baloon2[enemyIndex], transform.position, transform.rotation);
         
         // Play the spawning effect from all of the particle systems.
         foreach(ParticleSystem p in GetComponentsInChildren<ParticleSystem>())
         {
             p.Play();
         }
     }
 }
 
 

This script works fine but objects are spawning but not moving. How can I apply speed to them?

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

1 Reply

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

Answer by robertbu · Jan 17, 2014 at 05:53 PM

Not sure what direction you want them to move. Given the name 'baloon2', maybe up?

Replace 22 with:

 var go = Instantiate(baloon2[enemyIndex], transform.position, transform.rotation) as GameObject;
 go.rigidbody.AddForce(Vector3.up * someForce);

With a Rigidbody.mass of 1.0, start with 'someForce' at 500 and adjust from there.

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 Sweex · Jan 17, 2014 at 06:17 PM 0
Share

I misspelled balloon name so baloon2 is actually ok. I get error saying that there is no definition for AddForce

avatar image robertbu · Jan 17, 2014 at 06:18 PM 0
Share

I forgot the 'rigidbody'. I edited my answer.

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

Add an animation clip with AnimationClip using cSharp script 1 Answer

Current speed of an object? 5 Answers

Speed button for animations? 2 Answers

Not Run while Aiming; Slope Terrain Making Object Faster 0 Answers

Whats wrong with this ladder climb script? 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