Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by matthewse19 · Feb 23, 2016 at 02:23 PM · c#instantiaterigidbody2dvelocitybullet

Instantiate GameObject with Velocity

I am trying to instantiate a bullet from a script and then on that same script change the gameObject's velocity.

I know there are some answers on this but a lot of it is in JavaScript and the other stuff doesn't seem to be working for me.

 clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.identity);
 
         bullet.GetComponent<bullet> ().damage = GetComponentInChildren<newGun> ().damAge;
 
 
 
         if (GetComponent<testMove> ().facingRight == true) {
 
             clone.velocity = new Vector2(35, 0);
         } else {
 
             clone.velocity = new Vector2(-35,0);
         }

I don't know what clone should be defined as in order to also change its velocity

Comment
Add comment · Show 4
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 Magius96 · Feb 23, 2016 at 05:52 PM 0
Share

when you instantiate your bullet, ins$$anonymous$$d of using Quaternion.Identity, use the guns current rotation. Then set the velocity of the bullet.

clone.velocity = 35 * Vector3.Forward;

avatar image matthewse19 Magius96 · Feb 24, 2016 at 02:20 AM 0
Share

@$$anonymous$$agius96 Sorry I don't think I understand. I changed the velocity like you said, but I was getting an error. I still have clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.identity); written but is "clone" a filler word which I should change? And how should I create the object/what ever it is considered (what ever clone is) after being equal to the instantiate.

avatar image Glurth Magius96 · Feb 24, 2016 at 06:31 AM 2
Share
 clone.velocity = 35 * Vector3.Forward;

This will always equal (0,0,35). I think you meant:

 clone.velocity = 35 * clone.transform.forward;


matt- regarding the instantiate, I think this is what he was talking about.

 clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), gunTip.rotation);

or perhaps...

clone = Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), transform.rotation);

avatar image Magius96 Glurth · Feb 24, 2016 at 08:56 PM 0
Share

Ahh, yes, thank you Glurth.

1 Reply

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

Answer by matthewse19 · Feb 25, 2016 at 09:57 PM

Thanks for the help! Here's what I ended up writing if anyone who also needs help is wondering:

 GameObject clone;
 
 void shoot(){
         timer = 0;
         GetComponent<testMove>().ammo -=1;
          
         gunTip = transform.GetChild (2).FindChild ("GunTip").transform;
 
 
 
         clone = (GameObject) Instantiate (bullet, new Vector3(gunTip.position.x,gunTip.position.y,transform.position.z), Quaternion.Euler (0,0,0));
         
         clone.GetComponent<Rigidbody2D> ().velocity = 35 * transform.localScale.x * clone.transform.right;
 
         bullet.GetComponent<bullet> ().damage = GetComponentInChildren<newGun> ().damAge;
     }


I think there may have been some variables like timer and gunTip which I didn't define in this code but that wasn't the problem I had with. What I had to do was define clone as a GameObject and put (GameObject) in front of Instantiate to make it work. I also didn't instantiate the bullet based on the player's rotation because I am working in 2D (something that I probably should have mentioned). So instead for the bullets velocity I also multiplied it by the player's x scale because that is the axis I use to flip the player's sprite when they change direction.

Also I realized that there is some poor coding I did which I can shorten up but I was leaving it longer so I could adjust some individual values for testing.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How does one continuously move an object forward without using velocity? 1 Answer

Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers

Spawning objects with a fixed velocity in 3D? 0 Answers

RigidBody2D velocity randomly drops to zero 1 Answer

rigidbody.velocity.x doesn't work :( 2 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