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 cidmodder · Jun 17, 2012 at 03:13 AM · c#instantiatevector3shootingbullets

Instantiating Weirdly

I have a gun that is shooting bullets that spray the longer you shoot ( this question is the bases for what I'm doing http://answers.unity3d.com/questions/13003/fps-gun-accuracy-bullet-tracers.html). the only problem I have is that when I turn, the bullets start going spawning sideways from the gun until they reach about a 90 degree angle then they start coming back to the front of the gun. If I keep turning the same direction they move farther away from the gun again until they are at about the opposite 90 degrees again. If i keep rotating they move back to center and this keeps repeating. Something similar to a sine or cosine. Here is the script on the gun:

 public string weaponName;
     public GameObject bullet;
     public Rigidbody instantiatedProjectile;
     public int speed;
 
 void shoot(float shotSpread ){
         
         float vx = (1f - 2f * Random.value) * shotSpread;
         float vy = (1f - 2f * Random.value) * shotSpread;
         float vz = 1.0f;
         
         Vector3 direction = transform.TransformDirection(new Vector3(vx,vy,vz));
         
         GameObject bulletClone = Instantiate(bullet, transform.position, transform.rotation) as GameObject ;
         bulletClone.SendMessage("direction",direction);
         audio.Play();
         
     }

and on the bullet:

 public Vector3 dir;
     void Update () {
     
         
         transform.Translate(Vector3.forward + dir);
         
     }
 
 
     void direction(Vector3 direction ){
         
         dir = direction;
     }


the player has a script that all it does is send the shotSpread and calls the shot function on the gun when it shoots and shotSpread is just a float that increases as you hold the trigger. Pretty sure the problem isn't there so I've left it out for now. The gun is also a child of the camera so it follows it around. What is going on? is it with the Vector3.forward on the bullet? Any ideas for a workaround or a fix? Thanks in advance!

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

Answer by Berenger · Jun 17, 2012 at 03:42 AM

Couple things that need fixing.

First, SendMessage is a little expensive, you can set the function direction as public (or even better, make it a property) and then call bulletClone.GetComponent().direction = ...

Secondly, I doubt that the user is going to see the rotation of the bullet, unless it's reeeeeeeally slow, but for learning purposes you should make it look at that direction (LookAt, LookRotation, or forward =).

Then, you need to travel in that direction every frames. Vector3.forward is (0,0,1) in world coordinate, so when you turn it's lost. It goes like that (not finale version) :

 private void Update(){
     transform.Translate( tranform.forward ); // If you did the rotation
     //transform.Translate( dir); // If you didn't, make sure it's normalized.
 }

But this code is dependant of the frame rate, which mean it will travel really fast in good computers and really slow on shitty ones. To fix that, you need to multiply the vector by the delta time and a var to control it.

 private void Update(){
     transform.Translate( tranform.forward * Time.deltaTime * speed );
 }

Finaly, know that you won't have collision detection that way. Do it, and when it looks nice look the doc for rigidbody.

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 cidmodder · Jun 17, 2012 at 03:51 AM 0
Share

the first thing is very helpful i hadn't realised that. the second thing i'm not sure i explained myself well. the path the bullet travels is what is making a larger angle between the gun and what i think should be forward the more i turn. The third thing I know and had that set up before when the bullet just shot straight but took it out while i was playing with the bullet spread. Thanks for the re$$anonymous$$der though!

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

Problem with ScreenToWorldPoint and touches (C#) 2 Answers

Distribute terrain in zones 3 Answers

Unity 2D Rouge-Like game tutorial help 0 Answers

Reflect bullets in 3D world space 1 Answer

Multiple Cars not working 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