- Home /
AddForce with a constant velocity (not overtime)
So I've done reading an I'm still have somewhat of an Issue. I want to move my player object to the point that is click on the screen. I have everything working for screentoworld, the issue comes when i add a force to the player object. No matter the distance of the click I want the force that is applied to the object to be the same. Right now, if you click close the object the force is not the same as if you clicked as far way possible from the play object. Is there a way to do this? From what I understand,
Question 1
, AddForce takes a Vector3 which tells it the direction to move and the velocity, is that correct? I feel like I read that it was an angle, not a direction soecifically.Question 2
, I normalized it so the direction would remain the same but the Vector velocity would change and make the force a constant force, is that what .normailized does?pHitPoint is the Vector3 that holds the position of where the user clicked. transform.position is the position of the player, the object we want to add a force to.
rigidbody.AddForce((pHitPoint - transform.position).normalized *
(jumpForce * 2), ForceMode.Impulse);
Answer by Chris D · Jun 28, 2011 at 12:45 AM
A vector3 will always be just that: a vector with 3 components. What it supplies is the direction in X, Y, and Z to a point and the magnitude. What happens when you normalize a vector is that you're reducing it's total magnitude (the combination of the x/y/z components) to 1. There's no angles involved with a vector 3; you can use other functions to get some angles from them, but they're just distances in each axis when it comes down to it.
Sort of. Like I said above, it's just going to change the total magnitude of that vector's components to be 1. This allows you to know what kind of magnitudes you're going to be working with (always 1, then multiplied by the factor your choose) and prevents you from getting crazy results when doing vector addition/subtraction. If you didn't normalize and you did the same subtraction you did above, as the distances changes, the force you applied would also change (because the absolute magnitude of the vector you're supplying would be >1).
So if I were to Debug.log a normalized vector3, should it print (1.0, 1.0, 1.0)? $$anonymous$$aybe I'm not understanding how it deter$$anonymous$$es the magnitude.
I'm normalizing before I multiply by my jumpForce, but it's still not giving me constant speed.
No, it would output what would essentially be their proportional lengths. If you haven't dealt with 3D vectors before, check out the entry on Wikipedia. I wish I had a better guide to recommend, but I'm drawing a blank.
Think of it this way:
new pic inco$$anonymous$$g at a later date
in my (ultra sketchy) diagram, if you have a right-angle triangle with origin at the period (on a Cartesian x-y plane) with one side towards the x, one x->y and one y->., the magnitude (squared) of the vector2 .->y will be the square of .->x + square of x->y. IE x2+y2=h2 (ye old formula).
What you're doing when normalizing is reducing each component proportionally so that that magnitude always comes out to unit length (1). The individual components will not be 1 themselves.
Ok, that won't format properly, I'll give it another shot later. For now, look up vectors on wikipedia or somewhere more easily understood.
First of all, you don't have to normalize the Vector3. Normalizing a direction vector only clamps that vector's magnitude to a max of 1. If your direction vector was somehow altered, normalizing it won't help you undo that alteration.
But I believe your problem lies here:
pHitPoint is the Vector3 that holds the position of where the user clicked.
That leads me to believe that pHitPoint is just the Vector3 you get from accessing Input.mousePosition. When you retrieve the cursor position from Input.mousePosition, it will return a Vector3 with a depth value that's pretty much useless to the user most of the time. I think the depth value it returns is the near plane of the camera or something, I don't really remember off the top of my head. Anyway, if you want to use a "click" as the direction vector for a jump, then you will have to perform some extra steps so that you're actually getting the correct direction vector. The Camera class has a few methods that will do correct screenspace-to-worldspace for you, but you can also alter the direction vector yourself (assu$$anonymous$$g that your game is set in 2d and depth is constant).