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 Geta-Ve · Jun 26, 2012 at 07:22 AM · instantiatevelocityinitial

Instantiate with initial velocity

As a challenge to myself to learn Unity and Unity Scripting I am trying to recreate an old iOS game, Cube Runner (not to sell).

My initial goal has been to set up rudimentary controls for the player, instantiate the basic prefab to dodge, and subsequently set that prefab to head briskly toward the player, on, for instance, the Z axis.

I have complete the first two pieces thus far. I am able to move my basic player around, and I can instantiate my obstacle, however, I am unable to ascertain how to set an initial velocity.

My code thus far (for the instantiation). I realize how utterly simple this script is, but as I said, I am simply trying to learn. :) I appreciate any and all help.

 #pragma strict
 
 var newObject : Rigidbody;
 private var towerCount = 0;
 
 function Start () {
 
 }
 
 function FixedUpdate () 
 {
     var objectNum = Random.Range(0,2);
     
     for (var i = 0; i < objectNum; i++)
     {
         Instantiate(newObject, 
                         Vector3
                         (
                             Random.Range(-50.0,50.0),
                             Random.Range(0.0,0.0),
                             Random.Range(-50.0,50.0)
                         ), 
                         Quaternion.Euler
                         (
                             Random.Range(0.0,0.0),
                             Random.Range(0.0,360.0),
                             Random.Range(0.0,0.0)
                         )
                     );
         newObject.AddForce(1,0,0);
                     
         Debug.Log("Tower has been built!");
         towerCount++;
     }
 }

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
1
Best Answer

Answer by pheash · Jun 26, 2012 at 07:31 AM

The problem with newObject.AddForce(1,0,0) is that is tries adding that force to your prefab and not the object that you have just created. To fix this, assign the instantiated object to a variable:

         var objectClone = Instantiate(newObject, 
                  Vector3
                  (
                    Random.Range(-50.0,50.0),
                    Random.Range(0.0,0.0),
                    Random.Range(-50.0,50.0)
                  ), 
                  Quaternion.Euler
                  (
                    Random.Range(0.0,0.0),
                    Random.Range(0.0,360.0),
                    Random.Range(0.0,0.0)
                  )
               );
        objectClone.AddForce(1,0,0);
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 Geta-Ve · Jun 26, 2012 at 10:37 AM 0
Share

Thank you also. I will be trying out this method as well, for comparison. I really appreciate your time. :)

avatar image
1

Answer by hijinxbassist · Jun 26, 2012 at 07:38 AM

To move that prefab you should make a new script for it completely(this is an AI script in essence), regardless of if it moves in a steady direction or all around. So start by making a TowerMove script, if you havent already..make a prefab of this tower and add the newly created script to it. Drag that tower prefab onto the public variable on the script shown above. On this current script in the instantiate function add

 // Same code except with a var whatEver infront for reference
 var instance=Instantiate(newObject, 
              Vector3
              (
                Random.Range(-50.0,50.0)..yada.....

 //Now you can call that new object using the var whatEver
 //and add the player(gameobject this script is attached to) to 
 //the TowerMove's player variable
 instance.GetComponent(TowerMove).player=this.gameObject;

Now for the TowerMove script

 On TowerMove
 
 public var player:Transform;
 private var direction:Vector3;
 
 function Start()
 {
     direction=(player.position-transform.position).magnitude;
 }
 function Update()
 {
     transform.Translate(direction*Time.deltaTime);
 }

Takes one direction on start of the script(when prefab is spawned) in direction of the player.

Comment
Add comment · Show 5 · 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 hijinxbassist · Jun 26, 2012 at 07:46 AM 0
Share

Granted my solution is a bit complex, you will gain much needed experience and have a decent start for making your game more complex as learn. Happy coding

avatar image Geta-Ve · Jun 26, 2012 at 10:36 AM 0
Share

Thank you so much! I am definitely going to be trying this out soon. I will report back and let you know how it goes. :)

avatar image Geta-Ve · Jun 28, 2012 at 03:24 AM 0
Share

So I gave it a try, though I get a float conversion error. can't convert 'float' to vector3.

Am trying to work through it though. :) Thanks!

avatar image Geta-Ve · Jun 28, 2012 at 05:48 AM 0
Share

I am actually receiving two errors.

  1. Create.js Cannot convert UnityEngine.GameObject to UnityEngine.Transform

  2. Tower$$anonymous$$ove.js Cannot convert UnityEngine.GameObject to UnityEngine.Transform

avatar image hijinxbassist · Jun 28, 2012 at 07:49 AM 0
Share

The error is because you are assigning a GameObject to a Transform. Ins$$anonymous$$d when you say player= .. add .transform to the end

 player=GameObject.FindObjectWithTag("Player").transform;

or in the case of the first script(this might be the one actually)

 instance.GetComponent(Tower$$anonymous$$ove).player=this.transform;

before it said gameObject...it should reference the transform of the object in this case.

avatar image
0

Answer by Geta-Ve · Jul 14, 2012 at 01:56 AM

I actually ended up going a slightly different route with my code. As seen below.

Creation Script

 #pragma strict
 
 var newTower : Rigidbody;
 private var towerCount = 0;
 
 function Start () {}
 
 function FixedUpdate () 
 {
  var towerNum = Random.Range(0,2);
  
  for (var i = 0; i < towerNum; i++)
  {
  var towerSpawnPosition = Instantiate
  (newTower, 
  Vector3
  (
  Random.Range(-50,50),
  Random.Range(0,0),
  Random.Range(180,200)
  ), 
  Quaternion.Euler
  (
  Random.Range(0,0),
  Random.Range(0,0),
  Random.Range(0,0)
  )
  );
  
  Debug.Log("Tower has been built!");
  towerCount++;
  } 
 }

Movement Script

 #pragma strict
 
 public var player:Transform;
 private var direction:Vector3;
 
 function Start()
 {
     direction = transform.TransformDirection(0,0,-20);
 }
 function Update()
 {
     transform.Translate(direction*Time.deltaTime);
 }
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

6 People are following this question.

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

Related Questions

How can you instantiate a prefab with an initial burst of speed using ForceMode2D.Impulse? 2 Answers

Gun Projectile Shooting In Wrong Direction (Javascript) 1 Answer

Rotation and Velocity of bullets. 1 Answer

raycast and instantiat advice 0 Answers

instantiate finding upstream velocity 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