- Home /
Shoot towards mouse in Unity2D
I've been having trouble figuring out how to shoot a projectile in constant speed from the location of my character to the mouse position, and let it continue after reaching the mouse position, all while ignoring gravity, as in, a straight line.
I've managed to make the projectile be at the right angle, but couldn't make it travel towards, and through, the mouse.
Answer by Omberone · Jun 27, 2014 at 10:50 PM
Vector2 direction = target - myPos;
direction.Normalize();
GameObject projectile = (GameObject)Instantiate( projectilePrefab, myPos, Quaternion.identity);
projectile.rigidbody2D.velocity = direction * speed;
if you want to get the mouse cursor's position in world coordinates all you have to do is
Vector2 cursorInWorldPos = Camera.main.ScreenToWorldPoint( Input.mousePosition );
It doesnt appear to work, the bullets just kind of fall with slight movement towards the clicking position.
This is my code: public class Shooting : $$anonymous$$onoBehaviour {
public GameObject bullet;
public float speed = 5.0f;
// Use this for initialization
void Start () {
}
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y) );
Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
Vector2 direction = target - myPos;
direction.Normalize();
GameObject projectile = (GameObject)Instantiate( bullet, myPos, Quaternion.identity);
projectile.rigidbody2D.velocity = direction * speed;
}
}
}
EDIT: never $$anonymous$$d, i just forgot to change the bullet's mass and gravity.
But there is another problem, the bullet doesnt rotate towards its flight direction, how to i fix that?
Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg );
Well, it sort of worked, it now faces its direction with its body, rather than its tip. Its like the bullet hits the target with its broad side rather than its head, if that sort of makes sense.
this is my implementation:
if (Input.Get$$anonymous$$ouseButtonDown(0)) {
Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x, Input.mousePosition.y) );
Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
Vector2 direction = target - myPos;
direction.Normalize();
Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg );
GameObject projectile = (GameObject) Instantiate( bullet, myPos, rotation);
projectile.rigidbody2D.velocity = direction * speed;
}
Hey I used this code and it works great, but the bullet starts in the middle of my player. How do I get it to start at the tip of the player?
Thanks, this code really helped a lot =).
that depends on where your bullet is facing in the prefab =) Is the tip pointing up, right, left or down?
In the prefab it's facing up. Edit: got it! :D
Quaternion rotation = Quaternion.Euler( 0, 0, $$anonymous$$athf.Atan2 ( direction.y, direction.x ) * $$anonymous$$athf.Rad2Deg + 90 );
Answer by BenBL · Oct 13, 2017 at 03:06 PM
Anyone can explain Why we had to +1 in position.y? Thanks.
Vector2 myPos = new Vector2(transform.position.x,transform.position.y + 1);
It may have had something to do with the size and/or shape of the character SgtPancakes was using; you can safely remove it if it is annoying though =).
Your answer
Follow this Question
Related Questions
Move sprite with mouse click Unity2D 0 Answers
Looking and firing towards mouse position, 2d game. 3 Answers
How to array 2d skeleton characters vertically? 0 Answers
Transform.translate bullets problem 0 Answers
Unity 2D and Texture Memory. 1 Answer