- Home /
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
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;
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.
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.
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
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
Thank you so much! This worked excellent, using
Vector2 dir = ((Vector2)ray.origin - (Vector2)transform.position).normalized;
Thank you! Jonathan