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 /
This question was closed Jul 20, 2016 at 06:41 PM by matthrewp for the following reason:

Problem was solved.

avatar image
0
Question by matthrewp · Jul 19, 2016 at 04:18 PM · scripting problemphysicsbullet

bullets acting weird when spawn

When I spawn a bullet in-game, it sometimes moves to the left or right before going forward. Also, if I spawn one bullet going in one direction and another in a different direction, it seems to yank the other bullet in the direction it's going for a short distance. Here is a link to a video of this happening:
https://drive.google.com/open?id=0B_HzgsC9lP3nS2RkUC1xRkU1Tmc
Here is my bullet spawning script:
using UnityEngine; using System.Collections;

 public class BulletController : MonoBehaviour {

     public GameObject BulletSpawn;
     public GameObject Bullet;
     GameObject BulletClone;
     public float BulletSpeed;
     void Update()
     {
         if (Input.GetKeyDown("space") == true)
         {
             BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
         }
         Rigidbody rBody = GetComponent<Rigidbody>();
         rBody.velocity = BulletClone.transform.forward * BulletSpeed;
     }
 }

Why are my bullets acting weird?

Comment
Add comment · Show 6
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 Cherno · Jul 19, 2016 at 04:29 PM 0
Share

Perhaps they are collising with each other?

avatar image matthrewp Cherno · Jul 19, 2016 at 05:53 PM 0
Share

Not possible. Pressing the space bar only spawns one bullet.

avatar image OrbitGames · Jul 19, 2016 at 06:02 PM 0
Share

They might be colliding with the gun

avatar image Loreen · Jul 19, 2016 at 06:07 PM 0
Share

As someone already mentioned they might be colliding with the gun. Can you set their trigger off?

avatar image FlightOfOne · Jul 19, 2016 at 06:19 PM 0
Share

just saw your link to the video... As everyone said, it does look like it is hitting something. OR You or it's adding another force somewhere along the process.

avatar image matthrewp FlightOfOne · Jul 19, 2016 at 06:37 PM 0
Share

The only part of the player that effects the bullets are the red body, gray shield, and larger blue cylinder.

2 Replies

  • Sort: 
avatar image
1

Answer by FlightOfOne · Jul 19, 2016 at 06:08 PM

  1. All physics stuff should be done in FixedUpdate(),

  2. It is not good to directly change the velocity of a "Rigidbody", you should be using forces to get desired velocity,

  3. Try this instead:

    Instead of Update() use fixed.

     void FixedUpdate()
             {
              
                     if (Input.GetKeyDown("space") == true)
                     {
                         BulletClone = (GameObject)Instantiate(Bullet, BulletSpawn.transform.position, BulletSpawn.transform.rotation);
                         Rigidbody rBody = GetComponent<Rigidbody>();
                         rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed,ForceMode.Impulse);
                         //rBody.AddRelativeForce(BulletClone.transform.forward * BulletSpeed, ForceMode.VelocityChange);  //<---Note ForceMode.VelocityChange, try this mode also, it ignores the mass
                 }
         
         
             }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
Comment
Add comment · Show 4 · 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 matthrewp · Jul 19, 2016 at 07:53 PM 0
Share

The bullets are still not going straight.

avatar image FlightOfOne matthrewp · Jul 19, 2016 at 08:11 PM 0
Share

You should disable everything, I mean everything. better yet, start a new scene. Just 1 Empty object, your bullet, and obviously the scripts(s). Then try again.

Also, make sure the bullet is not a child of anything.

avatar image FlightOfOne matthrewp · Jul 19, 2016 at 08:19 PM 0
Share

Also be sure that you are not applying force every frame. $$anonymous$$ake sure the portion of code that applies force is inside the if(Input) statement.

avatar image FlightOfOne matthrewp · Jul 19, 2016 at 09:02 PM 0
Share

One last thing, do you have any weird scales set, of Collider offsets?

avatar image
0

Answer by mnmwert · Jul 19, 2016 at 09:21 PM

here is a script that should make the bullets fire right. but bring your spawn point out a little more.

 using UnityEngine;
 using System.Collections;
 
 public class shotSpeed : MonoBehaviour {
     public float speed;
     public Vector3 target;
     public Transform target1;
     // Use this for initialization
     void Start ()
     {
         target = target1.transform.position;
     }
     
     // Update is called once per frame
     void Update ()
     {
         
         var delta = speed * Time.deltaTime;
         transform.position = Vector3.MoveTowards(transform.position, target, delta); // move towards        //target
 
         if(transform.position == target)
         {
             Destroy(gameObject);
         }
     }
 }



 
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

Follow this Question

Answers Answers and Comments

85 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 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 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 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 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

Bullet Not Working 3 Answers

trouble making a fast projectile 2 Answers

Why are these object passing through each other? 1 Answer

Add velocity relative to ground? 1 Answer

RigidBody.velocity or transform.position 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