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 LethalJam · Dec 22, 2014 at 03:38 AM · rigidbodyvector3directionnormalizednormalize

Normalizing vector doesn't give a constant direction.

So I'm making a script that shoots gameobjects in the direction of the mouse cursor but I'm having a problem. I normalize the direction but the vector changes based on the distance from my character and the mouse position. Here's the code:

     void Update () 
     {
         // Save the current mouseposition.
         mousePos = Input.mousePosition;
         
         // Convert the mouseposition to a position in the game world.
         mousePos = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 20f));
 
         direction = Vector3.Normalize( mousePos - transform.position);
 
         if (Input.GetMouseButton(0))
         {
             Rigidbody bulletInstance = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
             bulletInstance.AddForce(direction * bulletspeed);
         }
     }

This of course means that the speed of the bullets differ based on the distance to the cursor. How do I fix this or rather, what am I doing wrong? Thanks.

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

2 Replies

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

Answer by Owen-Reynolds · Dec 22, 2014 at 08:06 AM

Your Q and your text don't seem to match up.

Line 9 does what you are asking. mousePos-position (as you write,) can be longer or shorter. Normalize "cancels out" the distance, leaving you with an always length 1 arrow pointing to mousePos.

Why do you think it's not working?

Your Q title ... of course normalizing doesn't give a constant direction. It's purpose is to give a constant length, while keeping whatever direction.

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 LethalJam · Jan 05, 2015 at 01:03 PM 0
Share

Thanks for the anwser. I did mess up my question because I wasn't sure how to explain my thoughts. I did however realise what I was doing wrong and all that so I solved it myself but thanks anyway!

avatar image
0

Answer by Rhandros · Dec 22, 2014 at 07:46 AM

You're using rigidbody.AddForce() on your bullet which will add speed based on its direction. When you instantiate it in your code your bullet is inheriting the gun's rotation so you need to make sure your bullet is also facing the gun's direction in your prefab otherwise it will go to a different direction.

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

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

How I can define the throw direction to a static GameObject? 1 Answer

rigidbody.velocity.normalized application 1 Answer

What is the difference between Normalize & Normalized 1 Answer

Vector3.Normalize not working for particular input? 2 Answers

Flipping a 2D sprite based on direction along the x axis (2D)? 2 Answers


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