• 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
Question by johajo · Feb 22, 2018 at 05:03 PM · 2d gamescripting beginner2d-physics

Projectile speed dependent of direction vector length?

Hi everyone, I have a question regarding my bullet speed. I've made a fire method which will trigger on mouseclick.

 public void Fire()
     {
         GameObject projec = Instantiate(proj, gunpoint.transform.position, gunpoint.transform.rotation) as GameObject; //Instaniate bullet
         Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition); //get point by ray
         Vector2 dir = (ray.origin - transform.position).normalized; //Get vector direction between player and mousepoint clicked
         projec.GetComponent<Rigidbody2D>().velocity = projectile.speed * dir * Time.deltaTime; //Set projectile velocitymoving in direction vector with speed from other projectile script.
     }

However, when clicking close to the player, the direction vector becomes small --> slow speed of projectiles.

If clicking with a greater distance from the player, the distance vector becomes greater and the projectiles travels faster.

Have I interpreted this correctly? How would you guys handle firing a projectile from a gunpoint rotating around the player against a mouse point?

Thank you all Jonathan

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Bunny83 · Feb 22, 2018 at 05:21 PM

Are you sure that's really the code that executes (forgot to save the recent changes)? The "normalized" at the end of this line:

 Vector2 dir = (ray.origin - transform.position).normalized;

will make the vector always have a length of "1.0". So what you describe would happen when you don't have that "normalized" at the end.


ps: You should remove that Time.deltaTime from your that line:

 projec.GetComponent<Rigidbody2D>().velocity = projectile.speed * dir * Time.deltaTime; 

It makes no sense. The velocity is a property / state of the rigidbody. You only need to multiply be deltaTime when you have any additive process that is executed every frame. You set the speed once and the speed should be constant.


edit


I just saw where your problem might come from. ray.origin and transform.position are Vector3 values. That means the subtraction of the two will result in a Vector3. You then normalize the vector3 and finally assign it to a vector3 variable. So the conversion from Vector3 to Vector2 happens at the very end. If your vectors have any difference in the z direction that would be accounted for as well when normalizing. However when you do the V3 --> V2 conversion this amount is no longer present.


To fix this you can do this:

 Vector2 dir = ((Vector2)ray.origin - (Vector2)transform.position).normalized;

or this:

 Vector2 dir = (ray.origin - transform.position);
 dir.Normalize(); // or dir = dir.normalized;
Comment
yummy81
johajo

People who like this

2 Show 8 · 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 meat5000 ♦ · Feb 22, 2018 at 05:27 PM 0
Share

Just to simplify here, normalising a vector is the key. It prevents the effects like running faster at a 30 degree angle in Half-life.

avatar image Bunny83 meat5000 ♦ · Feb 22, 2018 at 05:43 PM 1
Share

Half-life never had this problem as far as i remember ^^. There is / was an issue when climbing ladders sideways. However normal movement is properly clamped to max speed. See the code. However many other games (especially indy games) have this problem. The actual strafe-jump / bunnyhop glitch happens server side.

avatar image meat5000 ♦ Bunny83 · Feb 22, 2018 at 05:57 PM 0
Share

Yeah I used to run around at 30 degrees for this reason. Undoubtedly was patched out at some point, but I'd long stopped playing by then, probably. I mentioned half-life because in that game it was most pronounced and noticeable. At glance at the code suggests that running normally was simply slower, but I only got 30 seconds. I'll look again later. Thanks for the snippet! :P

Show more comments
avatar image johajo · Feb 22, 2018 at 05:57 PM 0
Share

Hey,

Thank you for the answer!

I have saved it - When debugging I get the direction vector to (0.0, 0.0), when clicking on the gunpoint (where the projectile is instansiated). If I click the edge of the screen, I get sometinhg like (0.6, 0.0)..

Debug added in code by:

     public void Fire()
     {
         GameObject projec = Instantiate(proj, gunpoint.transform.position, gunpoint.transform.rotation) as GameObject; //Instaniate bullet
         Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition); //get point by ray
         Vector2 dir = (ray.origin - transform.position).normalized; //Get vector direction between player and mousepoint clicked
         Debug.Log(dir);
         projec.GetComponent<Rigidbody2D>().velocity = projectile.speed * dir; //Set projectile translation moving in direction vector with speed from other projectile script.
     }

Thank you for the Time.deltaTime remark, that makes sense.

Jonathan

avatar image johajo johajo · Feb 22, 2018 at 06:06 PM 0
Share

And to calrify, the problem persists

avatar image Bunny83 johajo · Feb 22, 2018 at 06:16 PM 0
Share

Yep, i've edited my answer ^^

avatar image johajo · Feb 22, 2018 at 07:02 PM 0
Share

Thank you so much! This worked excellent, using

  Vector2 dir = ((Vector2)ray.origin - (Vector2)transform.position).normalized;

Thank you! Jonathan

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

89 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 avatar image avatar image avatar image avatar image

Related Questions

,Top Down Shooter Acceleration and Deceleration/velocity Movement 2 Answers

[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers

Tilemap Collider 2D not generating colliders...,Tilemap Colliders won't generate.... 0 Answers

Need suggestion for tossing game 0 Answers

Unintended bounce on 2D platformer 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