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 SVKsuli · Jul 29, 2014 at 04:01 AM · gameobjectinstantiateplayerspeedbullet

Instantiate bullet hitting me when accelerated

hi all, i need help with this, it confuses me. i have ship with weapon that shoot bullet front of ship, all is ok unless i move then bullet hit me. i don't know why doing it because when bullet is created it have speed bigger how ship but it look like position where the bullet is created is closer to ship with increasing speed. here is script what i use.

 #pragma strict
 
 var my_pos : Transform;        
 var ammo : GameObject[];
 static var spawnDelay = 0.1f;
 var time = 0f;
 static var Pspeed = 0f;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function FixedUpdate () {
     time += spawnDelay;
     if(Input.GetKey(KeyCode.Mouse0)){
         if(time > 1){
             var ammoIndex = 0;
             var bullet : GameObject = Instantiate(ammo[ammoIndex], transform.position, transform.rotation) ;
             bullet.transform.LookAt(my_pos);
             bullet.transform.rotation = transform.rotation;
             bullet.rigidbody2D.velocity = transform.up * Pspeed; //i added here player speed because i dont want to be faster how bullet.
             time = 0;
         }
     }
 }

this is weapon script it create bullet front of me(weapon is empty gameobject added to front of my ship and there is bullet created).

 #pragma strict
 
 var bulletSpeed : float;
 
 function Start () {
     
 }
 
 function Update () {
     Destroy(gameObject,1.2);
     transform.Translate(Vector3.up * bulletSpeed * Time.deltaTime); //and this is basic speed of bullet.
 }
 
 function OnCollisionEnter2D(){
     Destroy(gameObject);
 }

and this is bullet.

Comment
Add comment · Show 11
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 Kiwasi · Jul 29, 2014 at 09:15 AM 0
Share

You are doing some strange things here.

$$anonymous$$ove the bullet either by setting rigidbody.velocity, or by using transform.Translate Don't use both in the same game.

avatar image SVKsuli · Jul 29, 2014 at 12:42 PM 0
Share

i only try doing it this way but nothing is change it doing same when i have only one but one thing is when bullet dont have transform.translate and i change rigidbody.velocity for transform.translate it dont work, it only spawn bullet front of me and it dont have speed.

avatar image Bojidar · Jul 29, 2014 at 12:49 PM 0
Share

Do you have a rigidBody(not 2D) attached to your bullet. Also there is no reason to put Destroy(gameObject,1.2); in Update. Ins$$anonymous$$d, it must be in Start (you do not want ~200 timers in a line, waiting to destroy your bullet).

avatar image SVKsuli · Jul 29, 2014 at 12:55 PM 0
Share

no i have rigidbody2d, destroy(gameobject) work right.

avatar image Bojidar · Jul 29, 2014 at 01:00 PM 0
Share

If your game is 2D then it is right to have rigidbody2D (I just thought it is 3D).

Show more comments

1 Reply

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

Answer by Ejlersen · Jul 29, 2014 at 07:46 PM

Here are my thoughts on the problem.

  1. Use Update(), instead of FixedUpdate() to spawn your bullet.

Why? Because of execution order of physics and game logic. FixedUpdate is within physics update, where as Update() is after. You can see execution order here: http://docs.unity3d.com/Manual/ExecutionOrder.html

  1. Set rigidbody2d.velocity on instantiation and not both velocity and translate.

  2. Set OnDestroy on instantiation and not in Update(). Once will suffice.

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 SVKsuli · Jul 29, 2014 at 09:34 PM 0
Share

ah big thx Ejlarsen! this change FixedUpdate for Update work, i had to change it on player script and on weapon must FixedUpdate had to stay.

avatar image Bojidar · Jul 30, 2014 at 02:42 PM 0
Share

Also, to stop collision with your ship, you can add the speed of the ship to the velocity of the bullet (will not stop collisions when there is torque or wneh you are speeding, but.. it is the same in the real world....).

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

Issues with destroying a game object 2 Answers

Quick Bullet Instantiation Problem 2 Answers

GameObject Instantiated in Wrong Position 1 Answer

How to add an asset to a script-enabled public game object? 1 Answer

What's the easiest way to create an Instance Manager as to avoid calling Instantiate during game runtime? 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