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 /
This question was closed Jun 11, 2013 at 07:45 AM by Fattie for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Catlard · Jun 11, 2013 at 06:32 AM · instantiatetransformvelocitydirectionbullet

Rotation and Velocity of bullets.

Hey you all!

So, I'm working in 2D. I've got a bullet firing from a turret that can rotate. The bullet is supposed to determine its movement vector based on where the mouse was when it was fired -- but I'm having trouble getting the bullet to both rotate in the direction it's going and go in that direction at the same time. Basically, I can have one or the other, but not both -- it's because if I orientate the bullet to its movement direction at all, it changes the angle somehow. So, if I comment out the line that assigns the angle, the bullet moves correctly. But when it's in, the bullets rotate correctly. But they always end up going "down" on the y axis. Here's the function -- what am I doing wrong, do you think?

 public void Shoot(float accuracyVariance, ShotType type) {
         
         if(_currentChargeObject)
             Destroy(_currentChargeObject);
         
         //determine prefab.
         GameObject shotPrefab = _bulletShotPrefab;
         if(type == ShotType.Exploding)
             shotPrefab = _missilePrefab;
         
         //get correct pos.
         Vector3 pos = _bulletStartPoint.transform.position;
         pos.z = _markerCollisionZ.transform.position.z;
         
         
         
         //instantiate.
         GameObject shot = (GameObject) Instantiate(shotPrefab, pos, Quaternion.identity);      
         
         //add velocity.
         Vector3 firingVector = _normalisedMovementVector;
         if(accuracyVariance > 0)
             firingVector = GetFiringVectorWithInaccuracy(accuracyVariance,   _normalisedMovementVector);
         shot.GetComponent<BulletMovementBehavior>().UpdateVelocity(firingVector * _shotSpeed);
         
         //get correct rot.
         Vector3 rot = _bulletStartPoint.transform.eulerAngles;
         rot.z = VectorToATan(_bulletStartPoint.transform.position, _reticle.transform.position);
         shot.transform.eulerAngles = rot;
         //print(firingVector);
         
         
         //bullet behavior.
         if(type == ShotType.Exploding) 
             shot.GetComponent<MissileBehavior>()._explosionPoint = _reticle.transform.position;    
     
         GenerateShell();
             
     }

   
  public float VectorToATan(Vector3 currPos, Vector3 destPos) {
             float opp = currPos.x - destPos.x;
             float adj = currPos.y - destPos.y;
             return Mathf.Rad2Deg * Mathf.Atan2(opp, adj);
             
         }
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

  • Sort: 
avatar image
0
Best Answer

Answer by Catlard · Jun 11, 2013 at 07:38 AM

Figured it out. The bullet was moving by using transform.Translate() every frame. Instead, i'm just adding the vector to the position -- this works nicely. transform.Translate() by default seems to move in the local space.

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

15 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

Related Questions

Bullets Based on Orientation 1 Answer

Problem with Shooting Accuracy 0 Answers

Gun Projectile Shooting In Wrong Direction (Javascript) 1 Answer

Change the transform scale based on direction of motion 0 Answers

NullReferenceException Transform.Find 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