Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 DugelStudios · Jan 01, 2016 at 07:54 PM · physicsphysics2dnormalizednormalizenormalization

Vector3.normalize not behaving as expected?

So i've got to top down 2d game. If you right click the player shoots a bullet. I make the bullet shoot by doing the following:

 public virtual void shoot()
     {
         if(ShootingType == "normal")
         {
             Vector3 norm = CalculateAngle();
             this.GetComponent<Rigidbody2D>().velocity = norm*BulletSpeed;
         }
     }

 public virtual Vector3 CalculateAngle()
     {
         Vector3 m1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector3 t1 = transform.position;
         return (m1-t1).normalized;
     }

And for some reason when I click far away from the player the bullet shoots fast, and when I click close to the player the bullet shoots slow... Shouldn't the bullet velocity be the same for both cases because I normalized the vector to get a magnitude of 1? I don't know why I can't figure this out, I had a similar situation awhile ago and I got it to work... Any Help?

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
1
Best Answer

Answer by ryan_unity · Jan 01, 2016 at 09:08 PM

m1 is a 3D vector that's pointing "into" the world:

 Vector3 m1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);

As you're working in 2D, you want a normalized vector in 2D space. If your camera is facing along the Z-axis then you could just set m1.z = transform.position.z inside CalculateAngle, like so:

 public virtual Vector3 CalculateAngle() {
     Vector3 m1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     m1.z = transform.position.z;
     Vector3 t1 = transform.position;
     return (m1-t1).normalized;
 }
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 DugelStudios · Jan 01, 2016 at 10:14 PM 0
Share

Thank you! I forgot about that z position. I ended up just changing m1 and t1's type to Vector2 ins$$anonymous$$d of Vector3 so I can keep everything in 2D. Again, thank you!

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

43 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

Related Questions

.normalized sometimes returns 0 values, in a condition that only functions if value is not 0... 1 Answer

How often does the internal physics calculation get called? 1 Answer

Slow down object about to collide 1 Answer

Unity 2D - How to calculate weight/strain? 0 Answers

Jump faster and fall slower 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