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 aBs0lut3_z3r0 · Apr 11, 2014 at 06:06 PM · 2dvector3translatemove speed

Projectile speed changing by distance to target

I don't know why this is being such a pain but I cannot seem to get it working correctly.

I am in a 2d project, I have my player object in the bottom left corner of the camera view. I am taking click input from the mouse to fire a projectile at the mouse position. The aiming works fine, the problem I am having is that the speed of the projectile object is changing based on the distance of the click from the start point.

I am taking the mouse input, running ScreenToWorldPoint on it, subtracting the result from the player's transform.position (player.transform.position - Camera.main.ScreenToWorldPoint(inputPos)) and passing the normalized result to the projectile and letting it transform.translate(targetPos projectileSpeed Time.deltaTime)

It all works as I would expect EXCEPT for the difference in speed. If you click very close to the player's position the projectile moves VERY slowly.

PLEASE!! What am I doing wrong? Or not doing that I need to be?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Apr 11, 2014 at 06:09 PM

Posting your script would help with give an accurate answer, but the issue is that you are not normalizing the targetPos vector. The code should be something like this:

 targetPos = (player.transform.position - Camera.main.ScreenToWorldPoint(inputPos)).normalized;

Note the 'normalized' on the end. You could do in your translate call instead:

 transform.translate(targetPos.normalzied * projectileSpeed * Time.deltaTime);

Note that your 'projectileSpeed' will likely need to be increased.

Comment
Add comment · Show 7 · 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 aBs0lut3_z3r0 · Apr 11, 2014 at 06:23 PM 0
Share

Here is what I have got as far as code...

 if (Input.Get$$anonymous$$ouseButton(0))
         {
             lastInput = Input.mousePosition;
             lastScreenToWorld = Camera.main.ScreenToWorldPoint(lastInput);
             vectorFromStart = lastScreenToWorld - player.transform.position;
             normalVectorFromStart = vectorFromStart.normalized;
             Vector3 startPos = player.transform.position;
             GameObject s = (GameObject)Instantiate(playerShotObject, startPos, Quaternion.identity);
             player_shot_manager sm = (player_shot_manager)s.GetComponent("player_shot_manager");
             sm.moveDirection = normalVectorFromStart;
         }

Then the player_shot_manager

 void Update ()
 {
     if (moveDirection != null)
      transform.Translate(moveDirection * shotSpeed * Time.deltaTime);
 }

Trying your suggestion as posted resulted in two things, the shot going backwards (reversed vector...) but also, still responding to input the same way...

As you click closer and closer to the player's center from the edge of the screen, the slower and slower the projectiles get...

avatar image robertbu · Apr 11, 2014 at 06:31 PM 0
Share

On a quick read, your code appears to have things normalized correctly. As the your 'player_shot_manager' script do:

Debug.Log(moveDirection.magnitude);

It should output a value very close to 1.0. Also note that a Vector3 is a value type (struct), so you don't need the null check.

avatar image aBs0lut3_z3r0 · Apr 11, 2014 at 06:41 PM 0
Share

Hah, guess I was not thinking about the null check on the vector, force of habit I guess :)

As for the magnitude, was actually just looking at that. the normalized vector is always(or almost) 1, there is a small range that allows me to get a .9999999...

The interesting thing to me is that no matter what, I cannot get a magnitude on the un-normalized vector of less than 10, clicking right on the center point of the player gives me an even 10 on the mag... not really sure what to make of all this...

avatar image aBs0lut3_z3r0 · Apr 11, 2014 at 06:46 PM 0
Share

Nurr... Alright, got it... The screenToWorldPoint was always returning a z value of 10, not sure why, but I guess it was throwing off the math. setting it to 0 before any further math seems to work $$anonymous$$UCH better. However, even like that, if you click unreasonably close to the center point it does mess with things, but I mean REALLY REALLY close to the center...

avatar image robertbu · Apr 11, 2014 at 06:54 PM 0
Share

I think I know what is going on. You are not placing the projectile on the same 'z' plane as your target. How you are using ScreenToWroldPoint() places the object on the plane of the camera. That means some of your movement will be 'z' movement (movement along the camera forward), which is not noticeable using an Orthographic camera, and may not be very noticeable using a perspective camera depending on the FOV. The closer you click to the target, the higher the percentage of the vector magnitude will be 'z' movement. I'm not sure exactly how you have things setup and whether you are using an Orthographic or Perspective camera. I'm guessing Orthographic since the code is working somewhat. Assu$$anonymous$$g your camera is looking towards positive 'z', between lines 4 and 5 insert:

 lastCreenToWorld.z = player.transform.position.z;  

Note if you place a scene view with a top-down view beside your game view when you play, you will see what I mean about 'z' movement and the placement of your projectile relative to the target.

Show more comments

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

21 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

Related Questions

Simultaneously translating and rotating 2D sprite? 1 Answer

2D Animation does not start 1 Answer

How to get this object to move 2 Answers

Getting Vector3 in between two given Vector3 after given Distance. 2 Answers

Multiple Fire Points 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