- Home /
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!
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.
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...
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.
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...
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...
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.
Your answer
Follow this Question
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